RLMObject

@interface RLMObject : RLMObjectBase

RLMObject is a base class for model objects representing data stored in Realms.

Define your model classes by subclassing RLMObject and adding properties to be persisted. Then instantiate and use your custom subclasses instead of using the RLMObject class directly.

// Dog.h
@interface Dog : RLMObject
@property NSString *name;
@property BOOL      adopted;
@end

// Dog.m
@implementation Dog
@end //none needed

Supported property types

  • NSString
  • NSInteger, int, long, float, and double
  • BOOL or bool
  • NSDate
  • NSData
  • NSNumber<X>, where X is one of RLMInt, RLMFloat, RLMDouble or RLMBool, for optional number properties
  • RLMObject subclasses, to model many-to-one relationships.
  • RLMArray<X>, where X is an RLMObject subclass, to model many-to-many relationships.

Querying

You can initiate queries directly via the class methods: allObjects, objectsWhere:, and objectsWithPredicate:. These methods allow you to easily query a custom subclass for instances of that class in the default Realm.

To search in a Realm other than the default Realm, use the allObjectsInRealm:, objectsInRealm:where:, and objectsInRealm:withPredicate: class methods.

See

RLMRealm

Relationships

See our Cocoa guide for more details.

Key-Value Observing

All RLMObject properties (including properties you create in subclasses) are Key-Value Observing compliant, except for realm and objectSchema.

Keep the following tips in mind when observing Realm objects:

  1. Unlike NSMutableArray properties, RLMArray properties do not require using the proxy object returned from -mutableArrayValueForKey:, or defining KVC mutation methods on the containing class. You can simply call methods on the RLMArray directly; any changes will be automatically observed by the containing object.
  2. Unmanaged RLMObject instances cannot be added to a Realm while they have any observed properties.
  3. Modifying managed RLMObjects within -observeValueForKeyPath:ofObject:change:context: is not recommended. Properties may change even when the Realm is not in a write transaction (for example, when -[RLMRealm refresh] is called after changes are made on a different thread), and notifications sent prior to the change being applied (when NSKeyValueObservingOptionPrior is used) may be sent at times when you cannot begin a write transaction.
  • Initializes an unmanaged instance of a Realm object.

    Call addObject: on an RLMRealm instance to add an unmanaged object into that Realm.

    See

    [RLMRealm addObject:]

    Declaration

    Objective‑C

    - (nonnull instancetype)init;

    Swift

    init()
  • Initializes an unmanaged instance of a Realm object.

    Pass in an NSArray or NSDictionary instance to set the values of the object’s properties.

    Call addObject: on an RLMRealm instance to add an unmanaged object into that Realm.

    See

    [RLMRealm addObject:]

    Declaration

    Objective‑C

    - (nonnull instancetype)initWithValue:(nonnull id)value;

    Swift

    init(value: AnyObject)
  • Returns the class name for a Realm object subclass.

    Warning

    Do not override. Realm relies on this method returning the exact class name.

    Declaration

    Objective‑C

    + (nonnull NSString *)className;

    Swift

    class func className() -> String

    Return Value

    The class name for the model class.

  • Creates an instance of a Realm object with a given value, and adds it to the default Realm.

    If nested objects are included in the argument, createInDefaultRealmWithValue: will be recursively called on them.

    See

    defaultPropertyValues

    Declaration

    Objective‑C

    + (nonnull instancetype)createInDefaultRealmWithValue:(nonnull id)value;

    Swift

    class func createInDefaultRealmWithValue(value: AnyObject) -> Self

    Parameters

    value

    The value used to populate the object. This can be any key-value coding compliant object, or an array or dictionary returned from the methods in NSJSONSerialization, or an NSArray containing one element for each persisted 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 NSArray, all properties must be present, valid and in the same order as the properties defined in the model.

  • Creates an instance of a Realm object with a given value, and adds it to the specified Realm.

    If nested objects are included in the argument, createInRealm:withValue: will be recursively called on them.

    See

    defaultPropertyValues

    Declaration

    Objective‑C

    + (nonnull instancetype)createInRealm:(nonnull RLMRealm *)realm
                                withValue:(nonnull id)value;

    Swift

    class func createInRealm(realm: RLMRealm, withValue value: AnyObject) -> Self

    Parameters

    realm

    The Realm which should manage the newly-created object.

    value

    The value used to populate the object. This can be any key-value coding compliant object, or an array or dictionary returned from the methods in NSJSONSerialization, or an NSArray containing one element for each persisted 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 NSArray, all properties must be present, valid and in the same order as the properties defined in the model.

  • Creates or updates a Realm object within the default Realm.

    This method may only be called on Realm object types with a primary key defined. If there is already an object with the same primary key value in the default Realm, its values are updated and the object is returned. Otherwise, this method creates and populates a new instance of the object in the default Realm.

    If nested objects are included in the argument, createOrUpdateInDefaultRealmWithValue: will be recursively called on them if they have primary keys, createInDefaultRealmWithValue: if they do not.

    If the argument is a Realm object already managed by the default Realm, the argument’s type is the same as the receiver, and the objects have identical values for their persisted properties, this method does nothing.

    See

    defaultPropertyValues, primaryKey

    Declaration

    Objective‑C

    + (nonnull instancetype)createOrUpdateInDefaultRealmWithValue:(nonnull id)value;

    Swift

    class func createOrUpdateInDefaultRealmWithValue(value: AnyObject) -> Self

    Parameters

    value

    The value used to populate the object. This can be any key-value coding compliant object, or an array or dictionary returned from the methods in NSJSONSerialization, or an NSArray containing one element for each persisted 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 NSArray, all properties must be present, valid and in the same order as the properties defined in the model.

  • Creates or updates an Realm object within a specified Realm.

    This method may only be called on Realm object types with a primary key defined. If there is already an object with the same primary key value in the given Realm, its values are updated and the object is returned. Otherwise this method creates and populates a new instance of this object in the given Realm.

    If nested objects are included in the argument, createOrUpdateInRealm:withValue: will be recursively called on them if they have primary keys, createInRealm:withValue: if they do not.

    If the argument is a Realm object already managed by the given Realm, the argument’s type is the same as the receiver, and the objects have identical values for their persisted properties, this method does nothing.

    See

    defaultPropertyValues, primaryKey

    Declaration

    Objective‑C

    + (nonnull instancetype)createOrUpdateInRealm:(nonnull RLMRealm *)realm
                                        withValue:(nonnull id)value;

    Swift

    class func createOrUpdateInRealm(realm: RLMRealm, withValue value: AnyObject) -> Self

    Parameters

    realm

    The Realm which should own the object.

    value

    The value used to populate the object. This can be any key-value coding compliant object, or an array or dictionary returned from the methods in NSJSONSerialization, or an NSArray containing one element for each persisted 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 NSArray, all properties must be present, valid and in the same order as the properties defined in the model.

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

    Declaration

    Objective‑C

    @property (readonly, nonatomic, nullable) RLMRealm *realm;

    Swift

    var realm: RLMRealm? { get }
  • The object schema which lists the persisted properties for the object.

    Declaration

    Objective‑C

    @property (readonly, nonatomic) RLMObjectSchema *_Nonnull objectSchema;

    Swift

    var objectSchema: RLMObjectSchema { 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

    Objective‑C

    @property (readonly, getter=isInvalidated, nonatomic) BOOL invalidated;

    Swift

    var invalidated: Bool { get }
  • Returns an array of property names for properties which should be indexed.

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

    Declaration

    Objective‑C

    + (nonnull NSArray<NSString *> *)indexedProperties;

    Swift

    class func indexedProperties() -> [String]

    Return Value

    An array of property names.

  • Override this method to specify the default values to be used for each property.

    Declaration

    Objective‑C

    + (nullable NSDictionary *)defaultPropertyValues;

    Swift

    class func defaultPropertyValues() -> [NSObject : AnyObject]?

    Return Value

    A dictionary mapping property names to their default values.

  • Override this method to specify the name of a property to be used as the primary key.

    Only properties of types RLMPropertyTypeString and RLMPropertyTypeInt 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

    Objective‑C

    + (nullable NSString *)primaryKey;

    Swift

    class func primaryKey() -> String?

    Return Value

    The name of the property designated as the primary key.

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

    Declaration

    Objective‑C

    + (nullable NSArray<NSString *> *)ignoredProperties;

    Swift

    class func ignoredProperties() -> [String]?

    Return Value

    An array of property names to ignore.

  • Override this method to specify the names of properties that are non-optional (i.e. cannot be assigned a nil value).

    By default, all properties of a type whose values can be set to nil are considered optional properties. To require that an object in a Realm always store a non-nil value for a property, add the name of the property to the array returned from this method.

    Properties of RLMObject type cannot be non-optional. Array and NSNumber properties can be non-optional, but there is no reason to do so: arrays do not support storing nil, and if you want a non-optional number you should instead use the primitive type.

    Declaration

    Objective‑C

    + (nonnull NSArray<NSString *> *)requiredProperties;

    Swift

    class func requiredProperties() -> [String]

    Return Value

    An array of property names that are required.

  • Override this method to provide information related to properties containing linking objects.

    Each property of type RLMLinkingObjects must have a key in the dictionary returned by this method consisting of the property name. The corresponding value must be an instance of RLMPropertyDescriptor that describes the class and property that the property is linked to.

    return @{ @"owners": [RLMPropertyDescriptor descriptorWithClass:Owner.class propertyName:@"dogs"] };
    

    Declaration

    Objective‑C

    + (nonnull NSDictionary<NSString *, RLMPropertyDescriptor *> *)
        linkingObjectsProperties;

    Swift

    class func linkingObjectsProperties() -> [String : RLMPropertyDescriptor]

    Return Value

    A dictionary mapping property names to RLMPropertyDescriptor instances.

  • Returns all objects of this object type from the default Realm.

    Declaration

    Objective‑C

    + (nonnull RLMResults *)allObjects;

    Swift

    class func allObjects() -> RLMResults

    Return Value

    An RLMResults containing all objects of this type in the default Realm.

  • Returns all objects of this object type matching the given predicate from the default Realm.

    Declaration

    Objective‑C

    + (nonnull RLMResults *)objectsWhere:(nonnull NSString *)predicateFormat, ...;

    Parameters

    predicateFormat

    A predicate format string, optionally followed by a variable number of arguments.

    Return Value

    An RLMResults containing all objects of this type in the default Realm that match the given predicate.

  • Returns all objects of this object type matching the given predicate from the default Realm.

    Declaration

    Objective‑C

    + (nonnull RLMResults *)objectsWithPredicate:(nullable NSPredicate *)predicate;

    Swift

    class func objectsWithPredicate(predicate: NSPredicate?) -> RLMResults

    Parameters

    predicate

    The predicate with which to filter the objects.

    Return Value

    An RLMResults containing all objects of this type in the default Realm that match the given predicate.

  • Retrieves the single instance of this object type with the given primary key from the default Realm.

    Returns the object from the default Realm which has the given primary key, or nil if the object does not exist. This is slightly faster than the otherwise equivalent [[SubclassName objectsWhere:@"primaryKeyPropertyName = %@", key] firstObject].

    This method requires that primaryKey be overridden on the receiving subclass.

    See

    -primaryKey

    Declaration

    Objective‑C

    + (nullable instancetype)objectForPrimaryKey:(nullable id)primaryKey;

    Swift

    convenience init?(forPrimaryKey primaryKey: AnyObject?)

    Return Value

    An object of this object type, or nil if an object with the given primary key does not exist.

  • Returns all objects of this object type from the specified Realm.

    Declaration

    Objective‑C

    + (nonnull RLMResults *)allObjectsInRealm:(nonnull RLMRealm *)realm;

    Swift

    class func allObjectsInRealm(realm: RLMRealm) -> RLMResults

    Parameters

    realm

    The Realm to query.

    Return Value

    An RLMResults containing all objects of this type in the specified Realm.

  • Returns all objects of this object type matching the given predicate from the specified Realm.

    Declaration

    Objective‑C

    + (nonnull RLMResults *)objectsInRealm:(nonnull RLMRealm *)realm
                                     where:(nonnull NSString *)predicateFormat, ...;

    Parameters

    predicateFormat

    A predicate format string, optionally followed by a variable number of arguments.

    realm

    The Realm to query.

    Return Value

    An RLMResults containing all objects of this type in the specified Realm that match the given predicate.

  • Returns all objects of this object type matching the given predicate from the specified Realm.

    Declaration

    Objective‑C

    + (nonnull RLMResults *)objectsInRealm:(nonnull RLMRealm *)realm
                             withPredicate:(nullable NSPredicate *)predicate;

    Swift

    class func objectsInRealm(realm: RLMRealm, withPredicate predicate: NSPredicate?) -> RLMResults

    Parameters

    predicate

    A predicate to use to filter the elements.

    realm

    The Realm to query.

    Return Value

    An RLMResults containing all objects of this type in the specified Realm that match the given predicate.

  • Retrieves the single instance of this object type with the given primary key from the specified Realm.

    Returns the object from the specified Realm which has the given primary key, or nil if the object does not exist. This is slightly faster than the otherwise equivalent [[SubclassName objectsInRealm:realm where:@"primaryKeyPropertyName = %@", key] firstObject].

    This method requires that primaryKey be overridden on the receiving subclass.

    See

    -primaryKey

    Declaration

    Objective‑C

    + (nullable instancetype)objectInRealm:(nonnull RLMRealm *)realm
                             forPrimaryKey:(nullable id)primaryKey;

    Swift

    convenience init?(inRealm realm: RLMRealm, forPrimaryKey primaryKey: AnyObject?)

    Return Value

    An object of this object type, or nil if an object with the given primary key does not exist.

  • Returns YES if another Realm object instance points to the same object as the receiver in the Realm managing the receiver.

    For object types with a primary, key, isEqual: is overridden to use this method (along with a corresponding implementation for hash).

    Declaration

    Objective‑C

    - (BOOL)isEqualToObject:(nonnull RLMObject *)object;

    Swift

    func isEqualToObject(object: RLMObject) -> Bool

    Parameters

    object

    The object to compare the receiver to.

    Return Value

    A Boolean indicating whether the object represents the same object as the receiver.