RLMObject Class Reference
Inherits from | NSObject |
Declared in | RLMObject.h RLMObject.mm |
Overview
In Realm you define your model classes by subclassing RLMObject and adding properties to be persisted. You 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
,CGFloat
,int
,long
,float
, anddouble
BOOL
orbool
NSDate
NSData
- RLMObject subclasses, so you can have many-to-one relationships.
RLMArray<X>
, where X is an RLMObject subclass, so you can have many-to-many relationships.
Attributes for Properties
You can set which of these properties should be indexed, stored inline, unique, required as well as delete rules for the links by implementing the attributesForProperty: method.
You can set properties to ignore (i.e. transient properties you do not want persisted to a Realm) by implementing ignoredProperties.
You can set default values for properties by implementing defaultPropertyValues.
Querying
You can query an object directly via the class methods: allObjects, objectsWhere:, objectsOrderedBy:where: and objectForKeyedSubscript: These methods allow you to easily query a custom subclass for instances of this class in the default Realm. To search in a Realm other than the default Realm use the interface on an RLMRealm instance.
Relationships
See our Cocoa guide for more details.
Tasks
Creating & Initializing Objects
-
– init
-
– initWithObject:
-
+ className
-
+ createInDefaultRealmWithObject:
-
+ createInRealm:withObject:
-
realm
property -
objectSchema
property -
deletedFromRealm
property
Customizing your Objects
Getting & Querying Objects from the Default Realm
Querying Specific Realms
Serializing Objects to JSON
Properties
deletedFromRealm
Indicates if an object has been deleted from a Realm and can no longer be accessed.
@property (nonatomic, readonly, getter=isDeletedFromRealm) BOOL deletedFromRealm
Declared In
RLMObject.h
Class Methods
allObjects
Get all objects of this type from the default Realm.
+ (RLMArray *)allObjects
Return Value
An RLMArray of all objects of this type in the default Realm.
Declared In
RLMObject.h
allObjectsInRealm:
Get all objects of this type from the specified Realm.
+ (RLMArray *)allObjectsInRealm:(RLMRealm *)realm
Parameters
- realm
The Realm instance to query.
Return Value
An RLMArray of all objects of this type in the specified Realm.
Declared In
RLMObject.h
attributesForProperty:
Implement to set custom attributes for each property.
+ (RLMPropertyAttributes)attributesForProperty:(NSString *)propertyName
Parameters
- propertyName
Name of property for which attributes have been requested.
Return Value
Bitmask of property attributes for the given property.
Declared In
RLMObject.h
className
Helper to return the class name for an RLMObject subclass.
+ (NSString *)className
Return Value
The class name for the model class.
Declared In
RLMObject.h
createInDefaultRealmWithObject:
Create an RLMObject in the default Realm with a a given object.
+ (instancetype)createInDefaultRealmWithObject:(id)object
Parameters
- object
The object used to populate the object. This can be any key/value compliant object, or a JSON object such as those returned from the methods in NSJSONSerialization, or an NSArray with one object for each persisted property. An exception will be thrown if all required properties are not present or no default is provided.
When passing in an NSArray, all properties must be present, valid and in the same order as the properties defined in the model.
Discussion
Creates an instance of this object and adds it to the default Realm populating the object with the given object.
See Also
Declared In
RLMObject.h
createInRealm:withObject:
Create an RLMObject in a Realm with a given object.
+ (instancetype)createInRealm:(RLMRealm *)realm withObject:(id)object
Parameters
- realm
The Realm in which this object is persisted.
- object
The object used to populate the object. This can be any key/value compliant object, or a JSON object such as those returned from the methods in NSJSONSerialization, or an NSArray with one object for each persisted property. An exception will be thrown if all required properties are not present or no default is provided.
When passing in an NSArray, all properties must be present, valid and in the same order as the properties defined in the model.
Discussion
Creates an instance of this object and adds it to the given Realm populating the object with the given object.
See Also
Declared In
RLMObject.h
defaultPropertyValues
Implement to indicate the default values to be used for each property.
+ (NSDictionary *)defaultPropertyValues
Return Value
NSDictionary mapping property names to their default values.
Declared In
RLMObject.h
ignoredProperties
Implement to return an array of property names to ignore. These properties will not be persisted and are treated as transient.
+ (NSArray *)ignoredProperties
Return Value
NSArray of property names to ignore.
Declared In
RLMObject.h
objectsInRealm:where:
Get objects matching the given predicate for this type from the specified Realm.
+ (RLMArray *)objectsInRealm:(RLMRealm *)realm where:(NSString *)predicateFormat, ...
Parameters
- realm
The Realm instance to query.
- predicateFormat
The predicate format string which can accept variable arguments.
Return Value
An RLMArray of objects of the subclass type in the specified Realm that match the given predicate
Declared In
RLMObject.h
objectsInRealm:withPredicate:
Get objects matching the given predicate for this type from the specified Realm.
+ (RLMArray *)objectsInRealm:(RLMRealm *)realm withPredicate:(NSPredicate *)predicate
Parameters
- realm
The Realm instance to query.
- predicate
The predicate to filter the objects.
Return Value
An RLMArray of objects of the subclass type in the specified Realm that match the given predicate
Declared In
RLMObject.h
objectsWhere:
Get objects matching the given predicate for this type from the default Realm.
+ (RLMArray *)objectsWhere:(NSString *)predicateFormat, ...
Parameters
- predicateFormat
The predicate format string which can accept variable arguments.
Return Value
An RLMArray of objects of the subclass type in the default Realm that match the given predicate
Declared In
RLMObject.h
objectsWithPredicate:
Get objects matching the given predicate for this type from the default Realm.
+ (RLMArray *)objectsWithPredicate:(NSPredicate *)predicate
Parameters
- predicate
The predicate to filter the objects.
Return Value
An RLMArray of objects of the subclass type in the default Realm that match the given predicate
Declared In
RLMObject.h
Instance Methods
JSONString
Returns this object represented as a JSON string.
- (NSString *)JSONString
Return Value
JSON string representation of this object.
Declared In
RLMObject.h
init
Initialize a standalone RLMObject
- (instancetype)init
Discussion
Initialize an unpersisted instance of this object. Call addObject: on an RLMRealm to add standalone object to a realm.
See Also
Declared In
RLMObject.h