Object

@objc(RealmSwiftObject)
open class Object : RLMObjectBase, RealmCollectionValue

Object is a class used to define Realm model objects.

In Realm you define your model classes by subclassing Object and adding properties to be managed. You then instantiate and use your custom subclasses instead of using the Object class directly.

class Dog: Object {
    @objc dynamic var name: String = ""
    @objc dynamic var adopted: Bool = false
    let siblings = List<Dog>()
}

Supported property types

  • String, NSString
  • Int
  • Int8, Int16, Int32, Int64
  • Float
  • Double
  • Bool
  • Date, NSDate
  • Data, NSData
  • @objc enum which has been delcared as conforming to RealmEnum.
  • RealmOptional<Value> for optional numeric properties
  • Object subclasses, to model many-to-one relationships
  • List<Element>, to model many-to-many relationships

String, NSString, Date, NSDate, Data, NSData and Object subclass properties can be declared as optional. Int, Int8, Int16, Int32, Int64, Float, Double, Bool, enum, and List properties cannot. To store an optional number, use RealmOptional<Int>, RealmOptional<Float>, RealmOptional<Double>, or RealmOptional<Bool> instead, which wraps an optional numeric value. Lists cannot be optional at all.

All property types except for List and RealmOptional must be declared as @objc dynamic var. List and RealmOptional properties must be declared as non-dynamic let properties. Swift lazy properties are not allowed.

Note that none of the restrictions listed above apply to properties that are configured to be ignored by Realm.

Querying

You can retrieve all objects of a given type from a Realm by calling the objects(_:) instance method.

Relationships

See our Cocoa guide for more details.

  • Creates an unmanaged instance of a Realm object.

    Call add(_:) on a Realm instance to add an unmanaged object into that Realm.

    See

    Realm().add(_:)

    Declaration

    Swift

    public override required init()
  • Creates an unmanaged instance of a Realm object.

    The value argument is used to populate the object. It can be a key-value coding compliant object, an array or dictionary returned from the methods in NSJSONSerialization, or an Array containing one element for each managed property. An exception will be thrown if any required properties are not present and those properties were not defined with default values.

    When passing in an Array as the value argument, all properties must be present, valid and in the same order as the properties defined in the model.

    Call add(_:) on a Realm instance to add an unmanaged object into that Realm.

    Declaration

    Swift

    public convenience init(value: Any)

    Parameters

    value

    The value used to populate the object.

  • The Realm which manages the object, or nil if the object is unmanaged.

    Declaration

    Swift

    public var realm: Realm? { get }
  • The object schema which lists the managed properties for the object.

    Declaration

    Swift

    public var objectSchema: ObjectSchema { get }
  • Indicates if the object can no longer be accessed because it is now invalid.

    An object can no longer be accessed if the object has been deleted from the Realm that manages it, or if invalidate() is called on that Realm.

    Declaration

    Swift

    public override final var isInvalidated: Bool { get }
  • A human-readable description of the object.

    Declaration

    Swift

    open override var description: String { get }
  • Override this method to specify the name of a property to be used as the primary key.

    Only properties of types String and Int can be designated as the primary key. Primary key properties enforce uniqueness for each value whenever the property is set, which incurs minor overhead. Indexes are created automatically for primary key properties.

    Declaration

    Swift

    @objc
    open class func primaryKey() -> String?

    Return Value

    The name of the property designated as the primary key, or nil if the model has no primary key.

  • Override this method to specify the names of properties to ignore. These properties will not be managed by the Realm that manages the object.

    Declaration

    Swift

    @objc
    open class func ignoredProperties() -> [String]

    Return Value

    An array of property names to ignore.

  • Returns an array of property names for properties which should be indexed.

    Only string, integer, boolean, Date, and NSDate properties are supported.

    Declaration

    Swift

    @objc
    open class func indexedProperties() -> [String]

    Return Value

    An array of property names.

  • Returns or sets the value of the property with the given name.

    Declaration

    Swift

    @objc
    open subscript(key: String) -> Any? { get set }
  • Registers a block to be called each time the object changes.

    The block will be asynchronously called after each write transaction which deletes the object or modifies any of the managed properties of the object, including self-assignments that set a property to its existing value.

    For write transactions performed on different threads or in different processes, the block will be called when the managing Realm is (auto)refreshed to a version including the changes, while for local write transactions it will be called at some point in the future after the write transaction is committed.

    If no queue is given, notifications are delivered via the standard run loop, and so can’t be delivered while the run loop is blocked by other activity. If a queue is given, notifications are delivered to that queue instead. When notifications can’t be delivered instantly, multiple notifications may be coalesced into a single notification.

    Unlike with List and Results, there is no initial callback made after you add a new notification block.

    Only objects which are managed by a Realm can be observed in this way. You must retain the returned token for as long as you want updates to be sent to the block. To stop receiving updates, call invalidate() on the token.

    It is safe to capture a strong reference to the observed object within the callback block. There is no retain cycle due to that the callback is retained by the returned token and not by the object itself.

    Warning

    This method cannot be called during a write transaction, or when the containing Realm is read-only.

    Declaration

    Swift

    public func observe<T: Object>(on queue: DispatchQueue? = nil,
                                   _ block: @escaping (ObjectChange<T>) -> Void) -> NotificationToken

    Parameters

    queue

    The serial dispatch queue to receive notification on. If nil, notifications are delivered to the current thread.

    block

    The block to call with information about changes to the object.

    Return Value

    A token which must be held for as long as you want updates to be delivered.

  • Returns whether two Realm objects are the same.

    Objects are considered the same if and only if they are both managed by the same Realm and point to the same underlying object in the database.

    Note

    Equality comparison is implemented by isEqual(_:). If the object type is defined with a primary key, isEqual(_:) behaves identically to this method. If the object type is not defined with a primary key, isEqual(_:) uses the NSObject behavior of comparing object identity. This method can be used to compare two objects for database equality whether or not their object type defines a primary key.

    Declaration

    Swift

    public func isSameObject(as object: Object?) -> Bool

    Parameters

    object

    The object to compare the receiver to.

  • A publisher that emits Void each time the object changes.

    Despite the name, this actually emits after the collection has changed.

    Declaration

    Swift

    public var objectWillChange: Publishers.WillChange<Object> { get }
  • Indicates if this object is frozen.

    Declaration

    Swift

    public var isFrozen: Bool { get }
  • Returns a frozen (immutable) snapshot of this object.

    The frozen copy is an immutable object which contains the same data as this object currently contains, but will not update when writes are made to the containing Realm. Unlike live objects, frozen objects can be accessed from any thread.

    Warning

    Holding onto a frozen object for an extended period while performing write transaction on the Realm may result in the Realm file growing to large sizes. See Realm.Configuration.maximumNumberOfActiveVersions for more information.

    Warning

    This method can only be called on a managed object.

    Declaration

    Swift

    public func freeze() -> Self