Classes
The following classes are available globally.
-
RLMArray
is the container type in Realm used to define to-many relationships.Unlike an
NSArray
,RLMArray
s hold a single type, specified by theobjectClassName
property. This is referred to in these docs as the “type” of the array.When declaring an
RLMArray
property, the type must be marked as conforming to a protocol by the same name as the objects it should contain (see theRLM_ARRAY_TYPE
macro). In addition, the property can be declared using Objective‑C generics for better compile-time type safety.RLM_ARRAY_TYPE(ObjectType) ... @property RLMArray<ObjectType *><ObjectType> *arrayOfObjectTypes;
RLMArray
s can be queried with the same predicates asRLMObject
andRLMResult
s.RLMArray
s cannot be created directly.RLMArray
properties onRLMObject
s are lazily created when accessed, or can be obtained by querying a Realm.Key-Value Observing
RLMArray
supports array key-value observing onRLMArray
properties onRLMObject
subclasses, and theinvalidated
property onRLMArray
instances themselves is key-value observing compliant when theRLMArray
is attached to a managedRLMObject
(RLMArray
s on unmanagedRLMObject
s will never become invalidated).Because
See moreRLMArray
s are attached to the object which they are a property of, they do not require using the mutable collection proxy objects from-mutableArrayValueForKey:
or KVC-compatible mutation methods on the containing object. Instead, you can call the mutation methods on theRLMArray
directly.Declaration
Objective‑C
@interface RLMArray <RLMObjectType : RLMObject *> : NSObject<RLMCollection,NSFastEnumeration>
Swift
class RLMArray : NSObject
-
An
RLMSortDescriptor
stores a property name and a sort order for use withsortedResultsUsingDescriptors:
. It is similar toNSSortDescriptor
, but supports only the subset of functionality which can be efficiently run by Realm’s query engine.
See moreRLMSortDescriptor
instances are immutable.Declaration
Objective‑C
@interface RLMSortDescriptor : NSObject
Swift
class RLMSortDescriptor : NSObject
-
A
RLMCollectionChange
object encapsulates information about changes to collections that are reported by Realm notifications.RLMCollectionChange
is passed to the notification blocks registered with-addNotificationBlock
onRLMArray
andRLMResults
, and reports what rows in the collection changed since the last time the notification block was called.The change information is available in two formats: a simple array of row indices in the collection for each type of change, and an array of index paths in a requested section suitable for passing directly to
UITableView
’s batch update methods. A complete example of updating aUITableView
namedtv
:[tv beginUpdates]; [tv deleteRowsAtIndexPaths:[changes deletionsInSection:0] withRowAnimation:UITableViewRowAnimationAutomatic]; [tv insertRowsAtIndexPaths:[changes insertionsInSection:0] withRowAnimation:UITableViewRowAnimationAutomatic]; [tv reloadRowsAtIndexPaths:[changes modificationsInSection:0] withRowAnimation:UITableViewRowAnimationAutomatic]; [tv endUpdates];
All of the arrays in an
See moreRLMCollectionChange
are always sorted in ascending order.Declaration
Objective‑C
@interface RLMCollectionChange : NSObject
Swift
class RLMCollectionChange : NSObject
-
RLMMigration
instances encapsulate information intended to facilitate a schema migration.A
See moreRLMMigration
instance is passed into a user-definedRLMMigrationBlock
block when updating the version of a Realm. This instance provides access to the old and new database schemas, the objects in the Realm, and provides functionality for modifying the Realm during the migration.Declaration
Objective‑C
@interface RLMMigration : NSObject
Swift
class RLMMigration : NSObject
-
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 theRLMObject
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
, anddouble
BOOL
orbool
NSDate
NSData
NSNumber<X>
, whereX
is one ofRLMInt
,RLMFloat
,RLMDouble
orRLMBool
, for optional number propertiesRLMObject
subclasses, to model many-to-one relationships.RLMArray<X>
, whereX
is anRLMObject
subclass, to model many-to-many relationships.
Querying
You can initiate queries directly via the class methods:
allObjects
,objectsWhere:
, andobjectsWithPredicate:
. 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:
, andobjectsInRealm: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 forrealm
andobjectSchema
.Keep the following tips in mind when observing Realm objects:
- 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 theRLMArray
directly; any changes will be automatically observed by the containing object. - Unmanaged
RLMObject
instances cannot be added to a Realm while they have any observed properties. - Modifying managed
RLMObject
s 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 (whenNSKeyValueObservingOptionPrior
is used) may be sent at times when you cannot begin a write transaction.
Declaration
Objective‑C
@interface RLMObject : RLMObjectBase
Swift
class RLMObject
-
This class represents Realm model object schemas.
When using Realm,
RLMObjectSchema
instances allow performing migrations and introspecting the database’s schema.Object schemas map to tables in the core database.
See moreDeclaration
Objective‑C
@interface RLMObjectSchema : NSObject <NSCopying>
Swift
class RLMObjectSchema : NSObject, NSCopying
-
RLMProperty
instances represent properties persisted to Realm in the context of an object schema.When using Realm,
RLMProperty
instances allow performing migrations and introspecting the database’s schema.These property instances map to columns in the core database.
See moreDeclaration
Objective‑C
@interface RLMProperty : NSObject
Swift
class RLMProperty : NSObject
-
An
See moreRLMPropertyDescriptor
instance represents a specific property on a given class.Declaration
Objective‑C
@interface RLMPropertyDescriptor : NSObject
Swift
class RLMPropertyDescriptor : NSObject
-
An
RLMRealm
instance (also referred to asa Realm
) represents a Realm database.Realms can either be stored on disk (see
+[RLMRealm realmWithURL:]
) or in memory (seeRLMRealmConfiguration
).RLMRealm
instances are cached internally, and constructing equivalentRLMRealm
objects (for example, by using the same path or identifier) multiple times on a single thread within a single iteration of the run loop will normally return the sameRLMRealm
object.If you specifically want to ensure a
RLMRealm
instance 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.Warning
RLMRealm
instances are not thread safe and cannot be shared across threads or dispatch queues. Trying to do so will cause an exception to be thrown. You must call this method on each thread you want to interact with the Realm on. For dispatch queues, this means that you must call it in each block which is dispatched, as a queue is not guaranteed to run all of its blocks on the same thread.Declaration
Objective‑C
@interface RLMRealm : NSObject
Swift
class RLMRealm : NSObject
-
A token which is returned from methods which subscribe to changes to a Realm.
Change subscriptions in Realm return an
See moreRLMNotificationToken
instance, which can be used to unsubscribe from the changes. You must store a strong reference to the token for as long as you want to continue to receive notifications. When you wish to stop, call the-stop
method. Notifications are also stopped if the token is deallocated.Declaration
Objective‑C
@interface RLMNotificationToken : NSObject
Swift
class RLMNotificationToken : NSObject
-
An
RLMRealmConfiguration
instance describes the different options used to create an instance of a Realm.RLMRealmConfiguration
instances are just plainNSObject
s. UnlikeRLMRealm
s andRLMObject
s, they can be freely shared between threads as long as you do not mutate them.Creating configuration objects for class subsets (by setting the
See moreobjectClasses
property) can be expensive. Because of this, you will normally want to cache and reuse a single configuration object for each distinct configuration rather than creating a new object each time you open a Realm.Declaration
Objective‑C
@interface RLMRealmConfiguration : NSObject <NSCopying>
Swift
class RLMRealmConfiguration : NSObject, NSCopying
-
RLMResults
is an auto-updating container type in Realm returned from object queries. It represents the results of the query in the form of a collection of objects.RLMResults
can be queried using the same predicates asRLMObject
andRLMArray
, and you can chain queries to further filter results.RLMResults
always reflect the current state of the Realm on the current thread, including during write transactions on the current thread. The one exception to this is when usingfor...in
fast enumeration, which will always enumerate over the objects which matched the query when the enumeration is begun, even if some of them are deleted or modified to be excluded by the filter during the enumeration.RLMResults
are lazily evaluated the first time they are accessed; they only run queries when the result of the query is requested. This means that chaining several temporaryRLMResults
to sort and filter your data does not perform any extra work processing the intermediate state.Once the results have been evaluated or a notification block has been added, the results are eagerly kept up-to-date, with the work done to keep them up-to-date done on a background thread whenever possible.
See moreRLMResults
cannot be directly instantiated.Declaration
Objective‑C
@interface RLMResults <RLMObjectType : RLMObject *> : NSObject<RLMCollection,NSFastEnumeration>
Swift
class RLMResults : NSObject
-
RLMLinkingObjects
is an auto-updating container type. It represents a collection of objects that link to its parent object.For more information, please see the
Inverse Relationships
section in the documentation.Declaration
Objective‑C
@interface RLMLinkingObjects <RLMObjectType : RLMObject *> : RLMResults
Swift
class RLMLinkingObjects : RLMResults
-
RLMSchema
instances represent collections of model object schemas persisted to a Realm.When using Realm,
RLMSchema
instances allow performing migrations and introspecting the database’s schema.Schemas map to collections of tables in the core database.
See moreDeclaration
Objective‑C
@interface RLMSchema : NSObject <NSCopying>
Swift
class RLMSchema : NSObject, NSCopying