Realm

A Realm instance (also referred to as a realm) represents a Realm database.

Realms can either be stored on disk (see init(path:)) or in memory (see init(inMemoryIdentifier:)).

Realm instances are cached internally, and constructing equivalent Realm objects (with the same path or identifier) produces limited overhead.

If you specifically want to ensure a Realm object is destroyed (for example, if you wish to open a realm, check some property, and then possibly delete the realm file and re-open it), place the code which uses the realm within an autoreleasepool {} and ensure you have no other strong references to it.

  • Path to the file where this Realm is persisted.

    Declaration

    Swift

    public var path: String { return rlmRealm.path }
  • Indicates if this Realm was opened in read-only mode.

    Declaration

    Swift

    public var readOnly: Bool { return rlmRealm.readOnly }
  • The Schema used by this realm.

    Declaration

    Swift

    public var schema: Schema { return Schema(rlmRealm.schema) }
  • Returns a Configuration that can be used to create this Realm instance.

    Declaration

    Swift

    public var configuration: Configuration { return Configuration.fromRLMRealmConfiguration(rlmRealm.configuration) }
  • Undocumented

    Declaration

    Swift

    public final class Realm
  • Helper to perform actions contained within the given block inside a write transation.

    Declaration

    Swift

    public func write(block: (() -> Void))
  • Begins a write transaction in a Realm.

    Only one write transaction can be open at a time. Write transactions cannot be nested, and trying to begin a write transaction on a Realm which is already in a write transaction with throw an exception. Calls to beginWrite from Realm instances in other threads will block until the current write transaction completes.

    Before beginning the write transaction, beginWrite updates the Realm to the latest Realm version, as if refresh() was called, and generates notifications if applicable. This has no effect if the Realm was already up to date.

    It is rarely a good idea to have write transactions span multiple cycles of the run loop, but if you do wish to do so you will need to ensure that the Realm in the write transaction is kept alive until the write transaction is committed.

    Declaration

    Swift

    public func beginWrite()
  • Commits all writes operations in the current write transaction.

    After this is called, the Realm reverts back to being read-only.

    Calling this when not in a write transaction will throw an exception.

    Declaration

    Swift

    public func commitWrite()
  • Revert all writes made in the current write transaction and end the transaction.

    This rolls back all objects in the Realm to the state they were in at the beginning of the write transaction, and then ends the transaction.

    This restores the data for deleted objects, but does not reinstate deleted accessor objects. Any Objects which were added to the Realm will be invalidated rather than switching back to standalone objects. Given the following code:

    let oldObject = objects(ObjectType).first!
    let newObject = ObjectType()
    
    realm.beginWrite()
    realm.add(newObject)
    realm.delete(oldObject)
    realm.cancelWrite()
    

    Both oldObject and newObject will return true for invalidated, but re-running the query which provided oldObject will once again return the valid object.

    Calling this when not in a write transaction will throw an exception.

    Declaration

    Swift

    public func cancelWrite()
  • Indicates if this Realm is currently in a write transaction.

    Declaration

    Swift

    public var inWriteTransaction: Bool
  • Adds or updates an object to be persisted it in this Realm.

    When ‘update’ is ‘true’, the object must have a primary key. If no objects exist in the Realm instance with the same primary key value, the object is inserted. Otherwise, the existing object is updated with any changed values.

    When added, all linked (child) objects referenced by this object will also be added to the Realm if they are not already in it. If the object or any linked objects already belong to a different Realm an exception will be thrown. Use one of the create functions to insert a copy of a persisted object into a different Realm.

    The object to be added must be valid and cannot have been previously deleted from a Realm (i.e. invalidated must be false).

    Declaration

    Swift

    public func add(object: Object, update: Bool = false)
  • Adds or updates objects in the given sequence to be persisted it in this Realm.

    Declaration

    Swift

    public func add<S: SequenceType where S.Generator.Element: Object>(objects: S, update: Bool = false)
  • Create an Object with the given value.

    Creates or updates an instance of this object and adds it to the Realm populating the object with the given value.

    When ‘update’ is ‘true’, the object must have a primary key. If no objects exist in the Realm instance with the same primary key value, the object is inserted. Otherwise, the existing object is updated with any changed values.

    Declaration

    Swift

    public func create<T: Object>(type: T.Type, value: AnyObject = [:], update: Bool = false) -> T
  • Deletes the given object from this Realm.

    Declaration

    Swift

    public func delete(object: Object)
  • Deletes the given objects from this Realm.

    Declaration

    Swift

    public func delete<S: SequenceType where S.Generator.Element: Object>(objects: S)
  • Deletes all objects from this Realm.

    Declaration

    Swift

    public func deleteAll()
  • Returns all objects of the given type in the Realm.

    Declaration

    Swift

    public func objects<T: Object>(type: T.Type) -> Results<T>
  • Get an object with the given primary key.

    Returns nil if no object exists with the given primary key.

    This method requires that primaryKey() be overridden on the given subclass.

    Declaration

    Swift

    public func objectForPrimaryKey<T: Object>(type: T.Type, key: AnyObject) -> T?
  • Add a notification handler for changes in this Realm.

    Declaration

    Swift

    public func addNotificationBlock(block: NotificationBlock) -> NotificationToken
  • Remove a previously registered notification handler using the token returned from addNotificationBlock(_:)

    Declaration

    Swift

    public func removeNotification(notificationToken: NotificationToken)
  • Whether this Realm automatically updates when changes happen in other threads.

    If set to true (the default), changes made on other threads will be reflected in this Realm on the next cycle of the run loop after the changes are committed. If set to false, you must manually call -refresh on the Realm to update it to get the latest version.

    Even with this enabled, you can still call refresh() at any time to update the Realm before the automatic refresh would occur.

    Notifications are sent when a write transaction is committed whether or not this is enabled.

    Disabling this on a Realm without any strong references to it will not have any effect, and it will switch back to YES the next time the Realm object is created. This is normally irrelevant as it means that there is nothing to refresh (as persisted Objects, Lists, and Results have strong references to the containing Realm), but it means that setting Realm().autorefresh = false in application(_:didFinishLaunchingWithOptions:) and only later storing Realm objects will not work.

    Defaults to true.

    Declaration

    Swift

    public var autorefresh: Bool
  • Update a Realm and outstanding objects to point to the most recent data for this Realm.

    Declaration

    Swift

    public func refresh() -> Bool
  • Invalidate all Objects and Results read from this Realm.

    A Realm holds a read lock on the version of the data accessed by it, so that changes made to the Realm on different threads do not modify or delete the data seen by this Realm. Calling this method releases the read lock, allowing the space used on disk to be reused by later write transactions rather than growing the file. This method should be called before performing long blocking operations on a background thread on which you previously read data from the Realm which you no longer need.

    All Object, Results and List instances obtained from this Realm on the current thread are invalidated, and can not longer be used. The Realm itself remains valid, and a new read transaction is implicitly begun the next time data is read from the Realm.

    Calling this method multiple times in a row without reading any data from the Realm, or before ever reading any data from the Realm is a no-op. This method cannot be called on a read-only Realm.

    Declaration

    Swift

    public func invalidate()
  • Write an encrypted and compacted copy of the Realm to the given path.

    The destination file cannot already exist.

    Note that if this is called from within a write transaction it writes the current data, and not data when the last write transaction was committed.

    Declaration

    Swift

    public func writeCopyToPath(path: String, encryptionKey: NSData? = nil) throws