This is not the current version. View the latest documentation
If you’re looking to use Realm purely from Swift, you should consider using Realm Swift instead. The Realm Objective‑C and Realm Swift APIs are not interoperable and using them together is not supported.
Realm Objective‑C enables you to efficiently write your app’s model layer in a safe, persisted and fast way. Here’s what it looks like:
// Define your models like regular Objective‑C classes
@interface Dog : RLMObject
@property NSString *name;
@property NSData *picture;
@property NSInteger age;
@end
@implementation Dog
@end
RLM_ARRAY_TYPE(Dog)
@interface Person : RLMObject
@property NSString *name;
@property RLMArray<Dog *><Dog> *dogs;
@end
@implementation Person
@end
// Use them like regular Objective‑C objects
Dog *mydog = [[Dog alloc] init];
mydog.name = @"Rex";
mydog.age = 1;
mydog.picture = nil; // properties are nullable
NSLog(@"Name of dog: %@", mydog.name);
// Query Realm for all dogs less than 2 years old
RLMResults<Dog *> *puppies = [Dog objectsWhere:@"age < 2"];
puppies.count; // => 0 because no dogs have been added to the Realm yet
// Persist your data easily
RLMRealm *realm = [RLMRealm defaultRealm];
[realm transactionWithBlock:^{
[realm addObject:mydog];
}];
// Queries are updated in realtime
puppies.count; // => 1
// Query and update the result in another thread
dispatch_async(dispatch_queue_create("background", 0), ^{
Dog *theDog = [[Dog objectsWhere:@"age == 1"] firstObject];
RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
theDog.age = 3;
[realm commitWriteTransaction];
});
If you have an app that is presently using Core Data and have been considering switching to Realm, we recently published an article discussing how to go about doing this. Go check it out!
Getting Started
Download Realm Objective‑C or see the source on GitHub.
Prerequisites
- Apps using Realm can target: iOS 7 or later, macOS 10.9 or later, all versions of tvOS and watchOS.
- Xcode 7.3 or later required.
Installation
N.B.: Dynamic frameworks are not compatible with iOS 7. See “Static Framework” for iOS 7 support.
- Download the latest release of Realm and extract the zip.
- Go to your Xcode project’s “General” settings. Drag
Realm.framework
from theios/dynamic/
,osx/
,tvos/
orwatchos/
directory to the “Embedded Binaries” section. Make sure Copy items if needed is selected (except if using Realm on multiple platforms in your project) and click Finish. - In your unit test target’s “Build Settings”, add the parent path to
Realm.framework
in the “Framework Search Paths” section. - If using Realm with Swift, drag the file at
Swift/RLMSupport.swift
into the File Navigator of your Xcode project, checking the Copy items if needed checkbox. -
If using Realm in an iOS, watchOS or tvOS project, create a new “Run Script Phase” in your app’s target’s “Build Phases” and paste the following snippet in the script text field:
bash "${BUILT_PRODUCTS_DIR}/${FRAMEWORKS_FOLDER_PATH}/Realm.framework/strip-frameworks.sh"
This step is required to work around an App Store submission bug when archiving universal binaries.
- Install CocoaPods 0.39.0 or later.
- Run
pod repo update
to make CocoaPods aware of the latest available Realm versions. - In your Podfile, add
pod 'Realm'
to your app target andpod 'Realm/Headers'
to your test target. - From the command line, run
pod install
. - Use the
.xcworkspace
file generated by CocoaPods to work on your project! - If using Realm with Swift, drag the file at
Swift/RLMSupport.swift
into the File Navigator of your Xcode project, checking the Copy items if needed checkbox.
- Install Carthage 0.17.0 or later.
- Add
github "realm/realm-cocoa"
to your Cartfile. -
Run
carthage update
. To modify the Swift toolchain used to build, specify it via the--toolchain
argument. The--no-use-binaries
argument is also necessary to avoid Carthage pulling the prebuilt Swift 3.0 binaries. For example:carthage update --toolchain com.apple.dt.toolchain.Swift_2_3 --no-use-binaries
- Drag
Realm.framework
from the appropriate platform directory inCarthage/Build/
to the “Linked Frameworks and Libraries” section of your Xcode project’s “General” settings. -
iOS/tvOS/watchOS: On your application targets’ “Build Phases” settings tab, click the “+” icon and choose “New Run Script Phase”. Create a Run Script with the following contents:
/usr/local/bin/carthage copy-frameworks
and add the paths to the frameworks you want to use under “Input Files”, e.g.:
$(SRCROOT)/Carthage/Build/iOS/Realm.framework
This script works around an App Store submission bug triggered by universal binaries.
- If using Realm with Swift, drag the file at
Swift/RLMSupport.swift
into the File Navigator of your Xcode project, checking the Copy items if needed checkbox.
- Download the latest release of Realm and extract the zip.
- Drag
Realm.framework
from theios/static/
directory to the File Navigator of your Xcode project. Make sure Copy items if needed is selected and click Finish. - Click on your project in the Xcode File Navigator. Select your app’s target and go to the Build Phases tab. Under Link Binary with Libraries click + and add libc++.tbd and libz.tbd.
- If using Realm with Swift, drag the file at
Swift/RLMSupport.swift
into the File Navigator of your Xcode project, checking the Copy items if needed checkbox.
Importing the Realm Framework
At the top of your Objective‑C source files, use #import <Realm/Realm.h>
to import Realm Objective‑C and make it available for use with your code. At the top of your Swift source files (if you have any), use import Realm
. That’s all you need to get started!
Using Realm Objective‑C from Swift
If you’re looking to use Realm purely from Swift, you should consider using Realm Swift instead.
Realm Objective‑C is designed to work well with mixed Objective‑C and Swift projects. From Swift, you can do everything you can do when using Realm from Objective‑C, like defining models and using Realm’s Objective‑C API. However, there are a few things which you should do slightly differently than with pure Objective‑C projects:
RLMSupport.swift
We recommend you compile the Swift/RLMSupport.swift
file (which is also available in our release zip).
This file adds Sequence
conformance to Realm Objective‑C collection types and re-exposes Objective‑C methods that aren’t natively accessible from Swift like methods including variadic arguments.
Realm Objective‑C doesn’t include this file by default because that would force all users of Realm Objective‑C to include the hefty Swift dynamic libraries regardless of whether or not they use Swift in their app!
RLMArray
Properties
In Objective‑C, we rely on protocol conformance to make Realm aware of the contained object type in RLMArray
to-many relationships. In Swift, this kind of syntax isn’t possible. So instead, you should declare your RLMArray
properties with the following syntax:
class Person: RLMObject {
dynamic var dogs = RLMArray(objectClassName: Dog.className())
}
which is equivalent to the following in Objective‑C:
@interface Person : RLMObject
@property RLMArray<Dog *><Dog> *dogs;
@end
tvOS
Because writing to the Documents directory is prohibited on tvOS, the default Realm location is set to NSCachesDirectory
. However, please be mindful that tvOS can purge files in the Caches directory at any point, so we encourage you to rely on Realm as a rebuildable cache rather than storing important user data.
If you would like to share a Realm file between a tvOS app and a TV services extension (e.g. Top Shelf extension), you have to use the Library/Caches/
directory within the shared container for the application group.
// end declarations
RLMRealmConfiguration *configuration = [RLMRealmConfiguration defaultConfiguration];
configuration.fileURL = [[[NSFileManager defaultManager]
containerURLForSecurityApplicationGroupIdentifier:@"group.io.realm.examples.extension"]
URLByAppendingPathComponent:@"Library/Caches/default.realm"];
You can also bundle prebuilt Realm files in your app. However, be sure to comply with App Store guidelines, keeping your app under 200MB.
Please browse our tvOS examples for sample tvOS apps demonstrating how to use Realm as either an offline cache or with preloaded data.
Realm Browser
We also provide a standalone Mac app named Realm Browser to read and edit .realm databases.
You can generate a test database with sample data using the menu item Tools > Generate demo database.
If you need help finding your app’s Realm file, check this StackOverflow answer for detailed instructions.
The Realm Browser is available on the Mac App Store.
Xcode Plugin
Our Xcode plugin makes it easy to generate new Realm models.
The easiest way to install the Realm Xcode plugin is through Alcatraz under the name “RealmPlugin”. You can also install the plugin manually by opening plugin/RealmPlugin.xcodeproj
contained in the release zip and clicking build. You will need to quit and relaunch Xcode to see our plugin. If you use the Xcode menu to create a new file (File > New > File… — or ⌘N) you should see a new option to create a new Realm model.
API Reference
You can consult our full API reference for all classes, methods & more.
Examples
You can find example applications for both iOS and OS X in our release zip under examples/
, demonstrating how to use many features of Realm like migrations, how to use it with UITableViewController
s, encryption, command-line tools and much more.
Getting Help
- Need help with your code? Ask on StackOverflow. We actively monitor & answer questions on SO!
- Have a bug to report? Open an issue on our repo. If possible, include the version of Realm, a full log, the Realm file, and a project that shows the issue.
- Have a feature request? Open an issue on our repo. Tell us what the feature should do, and why you want the feature.
If you’re using a crash reporter (like Crashlytics or HockeyApp), make sure to enable log collection. Realm logs metadata information (but no user data) when throwing exceptions and in irrecoverable situations, and these messages can help debug when things go wrong.
Models
Realm data models are defined using regular Objective‑C classes with properties. Simply subclass RLMObject
or an existing model class to create your Realm data model objects. Realm model objects mostly function like any other Objective‑C objects - you can add your own methods and protocols to them and use them like you would any other object. The main restrictions are that you can only use an object on the thread which it was created, and you can’t access its ivars directly for any persisted properties.
If you have installed our Xcode Plugin there will be a nice template to create the interface and implementation files in the “New File…” dialog.
Relationships and nested data structures are modeled simply by including properties of the target type or RLMArray
s for typed lists of objects.
#import <Realm/Realm.h>
@class Person;
// Dog model
@interface Dog : RLMObject
@property NSString *name;
@property Person *owner;
@end
RLM_ARRAY_TYPE(Dog) // define RLMArray<Dog>
// Person model
@interface Person : RLMObject
@property NSString *name;
@property NSDate *birthdate;
@property RLMArray<Dog *><Dog> *dogs;
@end
RLM_ARRAY_TYPE(Person) // define RLMArray<Person>
// Implementations
@implementation Dog
@end // none needed
@implementation Person
@end // none needed
Since Realm parses all models defined in your code at launch, they must all be valid, even if they are never used.
When using Realm from Swift, the Swift.reflect(_:)
function is used to determine information about your models, which requires that calling init()
succeed. This means that all non-optional properties must have a default value.
See RLMObject for more details.
Supported Types
Realm supports the following property types: BOOL
, bool
, int
, NSInteger
, long
, long long
, float
, double
, NSString
, NSDate
, NSData
, and NSNumber
tagged with a specific type.
CGFloat
properties are discouraged, as the type is not platform independent.
You can use RLMArray<Object *><Object>
and RLMObject
subclasses to model relationships such as to-many and to-one.
RLMArray
s support compile-time Objective‑C generics if available (Xcode 7 or later). Here’s what the different components of the property definition mean and why they are useful:
RLMArray
: The property type.<Object *>
: The generic specialization. This helps prevent using the array with the wrong object type at compile-time.<Object>
: The protocol thisRLMArray
conforms to. This enables Realm to know how to specialize the schema of this model at runtime.
Relationships
RLMObject
s can be linked to each other by using RLMObject
and RLMArray
properties. RLMArray
s have an interface very similar to NSArray
and objects contained in a RLMArray
can be accessed using indexed subscripting. Unlike NSArray
s, RLMArray
s are typed and only hold RLMObject
s of a single subclass type. For more details see RLMArray
.
Assuming your Person model has already been defined (see above) let’s create another model called Dog:
// Dog.h
@interface Dog : RLMObject
@property NSString *name;
@end
To-One Relationships
For many-to-one or one-to-one relationships, simply declare a property with the type of your RLMObject
subclass:
// Dog.h
@interface Dog : RLMObject
// ... other property declarations
@property Person *owner;
@end
You can use this property like you would any other:
Person *jim = [[Person alloc] init];
Dog *rex = [[Dog alloc] init];
rex.owner = jim;
When using RLMObject
properties, you can access nested properties using normal property syntax. For example rex.owner?.address.country
will traverse the object graph and automatically fetch each object from Realm as needed.
To-Many Relationships
You can define a to-many relationship using RLMArray
properties. RLMArray
s contain other RLMObject
s of a single type and have an interface very similar to NSMutableArray
.
To add a “dogs” property on our Person
model that links to multiple dogs, we must first define an RLMArray<Dog>
type. This is done via a macro at the bottom of the corresponding model interface:
// Dog.h
@interface Dog : RLMObject
// ... property declarations
@end
RLM_ARRAY_TYPE(Dog) // Defines an RLMArray<Dog> type
The RLM_ARRAY_TYPE
macro creates an protocol to enable the use of the RLMArray<Dog>
syntax. You may have to forward-declare the model class if the macro is not placed at the bottom of the model interface.
You can then declare properties of the RLMArray<Dog>
type:
// Person.h
@interface Person : RLMObject
// ... other property declarations
@property RLMArray<Dog *><Dog> *dogs;
@end
You can access and assign RLMArray
properties as usual:
// Jim is owner of Rex and all dogs named "Fido"
RLMResults<Dog *> *someDogs = [Dog objectsWhere:@"name contains 'Fido'"];
[jim.dogs addObjects:someDogs];
[jim.dogs addObject:rex];
Note that although it’s possible to assign nil
to an RLMArray
property, this only “empties” the array rather than removing the array. This means that you can always add objects to an RLMArray
property, even after setting it to nil
.
RLMArray
properties are guaranteed to preserve their order of insertion.
Inverse Relationships
Links are unidirectional. So if a to-many property Person.dogs
links to a Dog
instance and a to-one property Dog.owner
links to Person
, these links are independent from one another. Appending a Dog
to a Person
instance’s dogs
property doesn’t automatically set the dog’s owner
property to this Person
. Because manually synchronizing pairs of relationships is error prone, complex and duplicates information, Realm provides linking objects properties to represent these inverse relationships.
With linking objects properties, you can obtain all objects that link to a given object from a specific property. For example, a Dog
object can have a property named owners
that contains all of the Person
objects that have this exact Dog
object in their dogs
property. This is done by making the owners
property of type RLMLinkingObjects
and then overriding +[RLMObject linkingObjectsProperties]
to indicate the relationship that owners
has with the Person
model objects.
@interface Dog : RLMObject
@property NSString *name;
@property NSInteger age;
@property (readonly) RLMLinkingObjects *owners;
@end
@implementation Dog
+ (NSDictionary *)linkingObjectsProperties {
return @{
@"owners": [RLMPropertyDescriptor descriptorWithClass:Person.class propertyName:@"dogs"],
};
}
@end
Optional Properties
By default, NSString *
, NSData *
, and NSDate *
properties allow you to set them to nil
. If you want to require that a value be present, you can override the +requiredProperties
method on your RLMObject
subclass. For example, with the following model definition, trying to set the person’s name to nil
will throw an exception, but setting their birthday to nil
is allowed:
@interface Person : RLMObject
@property NSString *name;
@property NSDate *birthday;
@end
@implementation Person
+ (NSArray *)requiredProperties {
return @[@"name"];
}
@end
Storing optional numbers is done using NSNumber *
properties. Because Realm uses different storage formats for different types of numbers, the property must be tagged with one of RLMInt
, RLMFloat
, RLMDouble
, or RLMBool
. All values assigned to the property will be converted to the specified type.
For example, if we wanted to store the person’s age rather than their birthday, while still allowing nil
when you don’t know the person’s age:
@interface Person : RLMObject
@property NSString *name;
@property NSNumber<RLMInt> *age;
@end
@implementation Person
+ (NSArray *)requiredProperties {
return @[@"name"];
}
@end
RLMObject
subclass properties always can be nil
and thus cannot be included in requiredProperties
, and RLMArray
does not support storing nil
.
Cheatsheet
This table provides a handy reference to declaring model properties.
Type | Non-optional | Optional |
---|---|---|
Bool | @property BOOL value; | @property NSNumber<RLMBool> *value; |
Int | @property int value; | @property NSNumber<RLMInt> *value; |
Float | @property float value; | @property NSNumber<RLMFloat> *value; |
Double | @property double value; | @property NSNumber<RLMDouble> *value; |
String | @property NSString *value; 1 | @property NSString *value; |
Data | @property NSData *value; 1 | @property NSData *value; |
Date | @property NSDate *value; 1 | @property NSDate *value; |
Object | n/a: must be optional | @property Object *value; |
List | @property RLMArray<Object *><Object> *value; | n/a: must be non-optional |
LinkingObjects | @property (readonly) RLMLinkingObjects<Object *> *value; 2 | n/a: must be non-optional |
1) Required properties of an Objective‑C reference type have to be declared in combination with:
@implementation MyModel
+ (NSArray *)requiredProperties {
return @[@"value"];
}
@end
2) Linking objects properties have to be declared in combination with a +linkingObjectsProperties
method:
@implementation MyModel
+ (NSDictionary *)linkingObjectsProperties {
return @{ @"property": [RLMPropertyDescriptor descriptorWithClass:Class.class propertyName:@"link"] };
}
@end
Property Attributes
Note that Realm ignores Objective‑C property attributes like nonatomic
, atomic
, strong
, copy
, weak
, etc. This is done because Realm has its own optimized storage semantics under the hood. So to avoid being misleading, we recommend writing models without any property attributes at all. However, if you do set property attributes, they will be used until an RLMObject
is added to a Realm. Custom names for getters and setters work normally regardless of whether or not an RLMObject
is managed by a Realm.
If using Realm Objective‑C with Swift, model properties need the dynamic var
attribute in order for these properties to become accessors for the underlying database data.
Indexed Properties
Override +indexedProperties
to set which properties in your model should be indexed:
@interface Book : RLMObject
@property float price;
@property NSString *title;
@end
@implementation Book
+ (NSArray *)indexedProperties {
return @[@"title"];
}
@end
Realm supports indexing for string, integer, boolean, and NSDate
properties.
Indexing a property will greatly speed up queries where the property is compared for equality (i.e. the =
and IN
operators), at the cost of slower insertions.
Default Property Values
Override +defaultPropertyValues
to provide default values every time an object is created.
@interface Book : RLMObject
@property float price;
@property NSString *title;
@end
@implementation Book
+ (NSDictionary *)defaultPropertyValues {
return @{@"price" : @0, @"title": @""};
}
@end
Auto-Updating Objects
RLMObject
instances are live, auto-updating views into the underlying data, which means objects never have to be refreshed. Modifying the properties of an object will immediately reflect in any other instances referring to the same object.
Dog *myDog = [[Dog alloc] init];
myDog.name = @"Fido";
myDog.age = 1;
[realm transactionWithBlock:^{
[realm addObject:myDog];
}];
Dog *myPuppy = [[Dog objectsWhere:@"age == 1"] firstObject];
[realm transactionWithBlock:^{
myPuppy.age = 2;
}];
myDog.age; // => 2
This aspect of RLMObject
not only keeps Realm fast and efficient, it allows your code to be simpler and more reactive. For example, if your UI code is dependent on a specific Realm object, you don’t need to worry about refreshing or re-fetching it before triggering a UI redraw.
You can subscribe to Realm notifications to know when Realm data in an object is updated, indicating when your app’s UI should be refreshed. Alternatively, you can use key-value observation to be notified when a specific property of an RLMObject
is updated.
Primary Keys
Override +primaryKey
to set the model’s primary key. Declaring a primary key allows objects to be looked up and updated efficiently and enforces uniqueness for each value. Once an object with a primary key is added to a Realm, the primary key cannot be changed.
@interface Person : RLMObject
@property NSInteger id;
@property NSString *name;
@end
@implementation Person
+ (NSString *)primaryKey {
return @"id";
}
@end
Ignored Properties
Override +ignoredProperties
to prevent Realm from persisting model properties. Realm won’t interfere with the regular operation of these properties: they’ll be backed by ivars and you can freely override their setters and getters.
@interface Person : RLMObject
@property NSInteger tmpID;
@property (readonly) NSString *name; // read-only properties are automatically ignored
@property NSString *firstName;
@property NSString *lastName;
@end
@implementation Person
+ (NSArray *)ignoredProperties {
return @[@"tmpID"];
}
- (NSString *)name {
return [NSString stringWithFormat:@"%@ %@", self.firstName, self.lastName];
}
@end
Ignored properties can’t be used in queries, they won’t retain their value when retrieving different instances the same object, won’t trigger notifications when changed, etc. They can still be KVO-observed, however.
Model Inheritance
Realm allows models to be further subclassed, allowing for much code reuse across models, but some Cocoa features that contribute to the runtime’s rich class polymorphism aren’t available. Here’s what’s possible:
- Class methods, instance methods and properties on parent classes are inherited in their child classes.
- Methods and functions that take parent classes as arguments can operate on subclasses.
The following is not possible:
- Casting between polymorphic classes (ie, subclass to subclass, subclass to parent, parent to subclass, etc.).
- Querying on multiple classes simultaneously.
- Multi-class containers (
RLMArray
andRLMResults
).
Adding this functionality to Realm is on the roadmap, and for the time being, we’ve provided some code samples for working around some of the more common patterns.
Alternatively, if your implementation allows it, we recommend using the following pattern of class composition to build up subclasses encompassing logic from other classes:
// Base Model
@interface Animal : RLMObject
@property NSInteger age;
@end
@implementation Animal
@end
// Models composed with Animal
@interface Duck : RLMObject
@property Animal *animal;
@property NSString *name;
@end
@implementation Duck
@end
@interface Frog : RLMObject
@property Animal *animal;
@property NSDate *dateProp;
@end
@implementation Frog
@end
// Usage
Duck *duck = [[Duck alloc] initWithValue:@{@"animal" : @{@"age" : @(3)}, @"name" : @"Gustav" }];
Collections
Realm has several types that help represent groups of objects, which we refer to as “Realm Collections”:
RLMResults
, a class representing objects retrieved from queries.RLMArray
, a class representing to-many relationships in models.RLMLinkingObjects
, a class representing inverse relationships in models.RLMCollection
, a protocol defining the common interface to which all Realm Collections conform.
Realm Collections conform to the RLMCollection
protocol, which ensures they behave consistently. This protocol inherits from NSFastEnumeration
so that it may be used in the same ways as other Foundation collections. Additional common Realm Collection APIs are declared in this protocol, such as querying, sorting and aggregate operations, among others. RLMArray
s have additional mutation operations that extend beyond the protocol interface such as adding and deleting objects.
Using the RLMCollection
protocol, you can write generic code that can operate on any Realm collection:
@implementation MyObject
- (void)operateOnCollection:(id<RLMCollection>)collection {
// Collection could be either RLMResults or RLMArray
NSLog(@"operating on collection of %@s", collection.objectClassName);
}
@end
Writes
All changes to an object (addition, modification and deletion) must be done within a write transaction.
Realm objects can be instantiated and used as unmanaged objects (i.e. not yet added to a Realm) just like regular Objective‑C objects. To share objects between threads or re-use them between app launches you must add them into a Realm, an operation which must be done within a write transaction.
Since write transactions incur non-negligible overhead, you should architect your code to minimize the number of write transactions.
Because write transactions could potentially fail like any other disk IO operations, both -[RLMRealm transactionWithBlock:]
& -[RLMRealm commitWriteTransaction]
optionally take an NSError
pointer parameter so you can handle and recover from failures like running out of disk space. There are no other recoverable errors. For brevity, our code samples don’t handle these errors but you certainly should in your production applications.
Creating Objects
When you have defined a model you can instantiate your RLMObject subclass and add the new instance to the Realm. Consider this simple model:
// Dog model
@interface Dog : RLMObject
@property NSString *name;
@property NSInteger age;
@end
// Implementation
@implementation Dog
@end
We can create new objects in several ways:
// (1) Create a Dog object and then set its properties
Dog *myDog = [[Dog alloc] init];
myDog.name = @"Rex";
myDog.age = 10;
// (2) Create a Dog object from a dictionary
Dog *myOtherDog = [[Dog alloc] initWithValue:@{@"name" : @"Pluto", @"age" : @3}];
// (3) Create a Dog object from an array
Dog *myThirdDog = [[Dog alloc] initWithValue:@[@"Pluto", @3]];
- The most obvious is to use the designated initializer to create an object. Please note that all required properties must be set before an object can be added to the Realm.
- Objects can also be created from dictionaries using appropriate keys and values.
- Finally,
RLMObject
subclasses can be instantiated using arrays. The values in the array have to be in the same order as the corresponding properties in the model.
Nested Objects
If an object has properties that are RLMObject
s or RLMArray
s, these can be set recursively using nested arrays and/or dictionaries. You simply replace each object with a dictionary or array representing its properties:
// Instead of using already existing dogs...
Person *person1 = [[Person alloc] initWithValue:@[@"Jane", @30, @[aDog, anotherDog]]];
// ...we can create them inline
Person *person2 = [[Person alloc] initWithValue:@[@"Jane", @30, @[@[@"Buster", @5],
@[@"Buddy", @6]]]];
This will work for any combination of nested arrays and dictionaries. Note that an RLMArray
may only contain RLMObject
s, not basic types such as NSString
.
Adding Objects
You can add an object to a Realm like so:
// Create object
Person *author = [[Person alloc] init];
author.name = @"David Foster Wallace";
// Get the default Realm
RLMRealm *realm = [RLMRealm defaultRealm];
// You only need to do this once (per thread)
// Add to Realm with transaction
[realm beginWriteTransaction];
[realm addObject:author];
[realm commitWriteTransaction];
After you have added the object to the Realm you can continue using it, and all changes you make to it will be persisted (and must be made within a write transaction). Any changes are made available to other threads that use the same Realm when the write transaction is committed.
Please note that writes block each other, and will block the thread they are made on if multiple writes are in progress. This is similar to other persistence solutions and we recommend that you use the usual best practice for this situation: offloading your writes to a separate thread.
Thanks to Realm’s MVCC architecture, reads are not blocked while a write transaction is open. Unless you need to make simultaneous writes from many threads at once, you should favor larger write transactions that do more work over many fine-grained write transactions. When you commit a write transaction to a Realm, all other instances of that Realm will be notified, and be updated automatically.
See RLMRealm and RLMObject for more details.
Updating Objects
Realm provides a few ways to update objects, all of which offer different tradeoffs depending on the situation. Choose which one is best for your situation:
Typed Updates
You can update any object by setting its properties within a write transaction.
// Update an object with a transaction
[realm beginWriteTransaction];
author.name = @"Thomas Pynchon";
[realm commitWriteTransaction];
Creating and Updating Objects With Primary Keys
If your model class includes a primary key, you can have Realm intelligently update or add objects based off of their primary key values using -[RLMRealm addOrUpdateObject:]
.
// Creating a book with the same primary key as a previously saved book
Book *cheeseBook = [[Book alloc] init];
cheeseBook.title = @"Cheese recipes";
cheeseBook.price = @9000;
cheeseBook.id = @1;
// Updating book with id = 1
[realm beginWriteTransaction];
[realm addOrUpdateObject:cheeseBook];
[realm commitWriteTransaction];
If a Book
object with a primary key value of ‘1’ already existed in the database, then that object would simply be updated. If it did not exist, then a completely new Book
object would be created and added to the database.
You can also partially update objects with primary keys by passing just a subset of the values you wish to update, along with the primary key:
// Assuming a "Book" with a primary key of `1` already exists.
[realm beginWriteTransaction];
[Book createOrUpdateInRealm:realm withValue:@{@"id": @1, @"price": @9000.0f}];
// the book's `title` property will remain unchanged.
[realm commitWriteTransaction];
You may not call those methods shown in this chapter (which end in OrUpdate
) with or on objects which don’t define a primary key.
Please note that when updating objects, NSNull
is still considered a valid value for optional properties. If you supply a dictionary with NSNull
property values, then these will be applied to your object and those properties will be emptied. To ensure you don’t experience any unplanned data loss, please make sure to provide only the properties you wish to update when using this method.
Key-Value Coding
RLMObject
, RLMResult
, and RLMArray
all conform to key-value coding (KVC). This can be useful when you need to determine which property to update at runtime.
Applying KVC to a collection is a great way to update objects in bulk without the overhead of iterating over a collection while creating accessors for every item.
RLMResults<Person *> *persons = [Person allObjects];
[[RLMRealm defaultRealm] transactionWithBlock:^{
[[persons firstObject] setValue:@YES forKeyPath:@"isFirst"];
// set each person's planet property to "Earth"
[persons setValue:@"Earth" forKeyPath:@"planet"];
}];
Deleting Objects
Pass the object to be deleted to the -[RLMRealm deleteObject:]
method within a write transaction.
// cheeseBook stored in Realm
// Delete an object with a transaction
[realm beginWriteTransaction];
[realm deleteObject:cheeseBook];
[realm commitWriteTransaction];
You can also delete all objects stored in a Realm. Note the Realm file will maintain its size on disk to efficiently reuse that space for future objects.
// Delete all objects from the realm
[realm beginWriteTransaction];
[realm deleteAllObjects];
[realm commitWriteTransaction];
Queries
Queries return an RLMResults
instance, which contains a collection of RLMObject
s. RLMResults
have an interface very similar to NSArray
and objects contained in a RLMResults
can be accessed using indexed subscripting. Unlike NSArray
s, RLMResults
are typed and only hold RLMObject
s of a single subclass type.
All queries (including queries and property access) are lazy in Realm. Data is only read when the properties are accessed.
Results to a query are not copies of your data: modifying the results of a query (within a write transaction) will modify the data on disk directly. Similarly, you can traverse your graph of relationships directly from the RLMObject
s contained in a RLMResults
.
Execution of a query is deferred until the results are used. This means that chaining several temporary RLMResults
to sort and filter your data does not perform extra work processing the intermediate state.
Once the query has been executed, or a notification block has been added, the RLMResults
is kept up to date with changes made in the Realm, with the query execution performed on a background thread when possible.
The most basic method for retrieving objects from a Realm is +[RLMObject allObjects]
, which returns a RLMResults of all RLMObject instances of the subclass type being queried from the default Realm.
RLMResults<Dog *> *dogs = [Dog allObjects]; // retrieves all Dogs from the default Realm
Filtering
If you’re familiar with NSPredicate
, then you already know how to query in Realm. RLMObjects
, RLMRealm
, RLMArray
, and RLMResults
all provide methods that allow you to query for specific RLMObject
instances by simply passing in an NSPredicate
instance, predicate string, or predicate format string just as you would when querying an NSArray
.
For example, the following would extend our earlier example by calling [RLMObject objectsWhere:]
to retrieve all tan-colored dogs whose names begin with ‘B’ from the default Realm:
// Query using a predicate string
RLMResults<Dog *> *tanDogs = [Dog objectsWhere:@"color = 'tan' AND name BEGINSWITH 'B'"];
// Query using an NSPredicate
NSPredicate *pred = [NSPredicate predicateWithFormat:@"color = %@ AND name BEGINSWITH %@",
@"tan", @"B"];
tanDogs = [Dog objectsWithPredicate:pred];
See Apple’s Predicates Programming Guide for more information about building predicates and use our NSPredicate Cheatsheet. Realm supports many common predicates:
- The comparison operands can be property names or constants. At least one of the operands must be a property name.
- The comparison operators ==, <=, <, >=, >, !=, and BETWEEN are supported for int, long, long long, float, double, and NSDate property types. Such as age == 45
- Identity comparisons ==, !=, e.g.
[Employee objectsWhere:@"company == %@", company]
- The comparison operators == and != are supported for boolean properties.
- For
NSString
andNSData
properties, we support the ==, !=, BEGINSWITH, CONTAINS, and ENDSWITH operators, such as name CONTAINS ‘Ja’ - For
NSString
properties, the LIKE operator may be used to compare the left hand property with the right hand expression:?
and*
are allowed as wildcard characters, where?
matches 1 character and*
matches 0 or more characters. Such asvalue LIKE '?bc*'
matching strings like “abcde” and “cbc”. - Case insensitive comparisons for strings, such as name CONTAINS[c] ‘Ja’. Note that only characters “A-Z” and “a-z” will be ignored for case.
- Realm supports the following compound operators: “AND”, “OR”, and “NOT”. Such as name BEGINSWITH ‘J’ AND age >= 32
- The containment operand IN such as name IN {‘Lisa’, ‘Spike’, ‘Hachi’}
- Nil comparisons ==, !=, e.g.
[Company objectsWhere:@"ceo == nil"]
. Note that Realm treatsnil
as a special value rather than the absence of a value, so unlike with SQLnil
equals itself. - ANY comparisons, such as ANY student.age < 21
- The aggregate expressions
@count
,@min
,@max
,@sum
and@avg
are supported onRLMArray
andRLMResults
properties, e.g.[Company objectsWhere:@"employees.@count > 5"]
to find all companies with more than five employees. - Subqueries are supported with the following limitations:
@count
is the only operator that may be applied to the SUBQUERY expression.- The
SUBQUERY(…).@count
expression must be compared with a constant. - Correlated subqueries are not yet supported.
For more, see [RLMObject objectsWhere:]
.
Sorting
RLMResults
allows you to specify a sort criteria and order based on a single or multiple properties or key paths. For example, the following calls sorts the dogs returned from the example above alphabetically by name:
// Sort tan dogs with names starting with "B" by name
RLMResults<Dog *> *sortedDogs = [[Dog objectsWhere:@"color = 'tan' AND name BEGINSWITH 'B'"]
sortedResultsUsingKeyPath:@"name" ascending:YES];
The key path may also be the property of a to-one relationship:
RLMResults<Person *> *dogOwners = [Person allObjects];
RLMResults<Person *> *ownersByDogAge = [dogOwners sortedResultsUsingKeyPath:@"dog.age" ascending:YES];
For more, see [RLMResults sortedResultsUsingKeyPath:ascending:]
.
Note that the order of Results
is only guaranteed to stay consistent when the query is sorted. For performance reasons, insertion order is not guaranteed to be preserved. If you need to maintain order of insertion, some solutions are proposed here.
Chaining
One unique property of Realm’s query engine is the ability to chain queries with very little transactional overhead when compared to traditional databases that require a separate trip to the database server for each successive query.
For example, if we wanted a result set for just tan colored dogs, and tan colored dogs whose names also start with ‘B’, you might chain two queries like this:
RLMResults<Dog *> *tanDogs = [Dog objectsWhere:@"color = 'tan'"];
RLMResults<Dog *> *tanDogsWithBNames = [tanDogs objectsWhere:@"name BEGINSWITH 'B'"];
Auto-Updating Results
RLMResults
instances are live, auto-updating views into the underlying data, which means results never have to be re-fetched. They 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 using for...in
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<Dog *> *puppies = [Dog objectsInRealm:realm where:@"age < 2"];
puppies.count; // => 0
[realm transactionWithBlock:^{
[Dog createInRealm:realm withValue:@{@"name": @"Fido", @"age": @1}];
}];
puppies.count; // => 1
This applies to all RLMResults
: all objects, filtered and chained.
This property of RLMResults
not only keeps Realm fast and efficient, it allows your code to be simpler and more reactive. For example, if your view controller relies on the results of a query, you can store the RLMResults
in a property and access it without having to make sure to refresh its data prior to each access.
You can subscribe to Realm notifications to know when Realm data is updated, indicating when your app’s UI should be refreshed for example, without having to re-fetch your RLMResults
.
Since results are auto-updating, it’s important to not rely on indices and counts staying constant. The only time a RLMResults
is frozen is when fast-enumerating over it, which makes it possible to mutate the objects matching a query while enumerating over it:
[realm beginWriteTransaction];
for (Person *person in [Person objectsInRealm:realm where:@"age == 10"]) {
person.age++;
}
[realm commitWriteTransaction];
Alternatively, use key-value coding to perform operations on RLMResults
.
Limiting Results
Most other database technologies provide the ability to ‘paginate’ results from queries (such as the ‘LIMIT’ keyword in SQLite). This is often done out of necessity to avoid reading too much from disk, or pulling too many results into memory at once.
Since queries in Realm are lazy, performing this sort of paginating behavior isn’t necessary at all, as Realm will only load objects from the results of the query once they are explicitly accessed.
If for UI-related or other implementation reasons you require a specific subset of objects from a query, it’s as simple as taking the RLMResults
object, and reading out only the objects you need.
// Loop through the first 5 Dog objects
// restricting the number of objects read from disk
RLMResults<Dog *> *dogs = [Dog allObjects];
for (NSInteger i = 0; i < 5; i++) {
Dog *dog = dogs[i];
// ...
}
Realms
The Default Realm
You may have noticed so far that we have initialized access to our realm
variable by calling [RLMRealm defaultRealm]
. That method returns an RLMRealm
object that maps to a file called “default.realm” under the Documents folder (iOS) or Application Support folder (OS X) of your app.
Many methods in Realm’s API have both a version that accepts an RLMRealm
instance, and a convenience version that uses the default Realm. For example, [RLMObject allObjects]
is equivalent to [RLMObject allObjectsInRealm:[RLMRealm defaultRealm]]
.
Please note that the default Realm constructor and default Realm convenience methods don’t allow error handling; you should only use them when initializing the Realm cannot fail. See error handling for details.
Realm Configuration
Configuring things like where your Realm files are stored is done through RLMRealmConfiguration
. The configuration can either be passed to [RLMRealm realmWithConfiguration:config error:&err]
each time you need a Realm instance, or you can set the configuration to use for the default Realm with [RLMRealmConfiguration setDefaultConfiguration:config]
.
For example, suppose you have an application where users have to log in to your web backend, and you want to support quickly switching between accounts. You could give each account its own Realm file that will be used as the default Realm by doing the following:
@implementation SomeClass
+ (void)setDefaultRealmForUser:(NSString *)username {
RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
// Use the default directory, but replace the filename with the username
config.fileURL = [[[config.fileURL URLByDeletingLastPathComponent]
URLByAppendingPathComponent:username]
URLByAppendingPathExtension:@"realm"];
// Set this as the configuration used for the default Realm
[RLMRealmConfiguration setDefaultConfiguration:config];
}
@end
Other Realms
It’s sometimes useful to have multiple Realms persisted at different locations. For example, you may want to bundle some data with your application in a Realm file, in addition to your main Realm. You can do this with the following code:
RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
// Get the URL to the bundled file
config.fileURL = [[NSBundle mainBundle] URLForResource:@"MyBundledData" withExtension:@"realm"];
// Open the file in read-only mode as application bundles are not writeable
config.readOnly = YES;
// Open the Realm with the configuration
RLMRealm *realm = [RLMRealm realmWithConfiguration:config error:nil];
// Read some data from the bundled Realm
RLMResults<Dog *> *dogs = [Dog objectsInRealm:realm where:@"age > 5"];
Please note that if a custom URL is used to initialize a Realm, it must be in a location with write permissions. The most common location to store writable Realm files is the “Documents” directory on iOS and the “Application Support” directory on OS X. Please respect Apple’s iOS Data Storage Guidelines, which recommend that documents that can be regenerated be stored in the <Application_Home>/Library/Caches
directory.
In-Memory Realms
Normally Realms are persisted to disk, but you can also create ones which operate purely in memory by setting the inMemoryIdentifier
rather than the fileURL
on your RLMRealmConfiguration
.
RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
config.inMemoryIdentifier = @"MyInMemoryRealm";
RLMRealm *realm = [RLMRealm realmWithConfiguration:config error:nil];
In-memory Realms do not save data across app launches, but all other features of Realm will work as expected, including querying, relationships and thread-safety. This is a useful option if you need flexible data access without the overhead of disk persistence.
In-memory Realms create several files in a temporary directory for coordinating things like cross-process notifications. No data is actually written to the files unless the operating system needs to swap to disk due to memory pressure.
Notice: When all in-memory Realm instances with a particular identifier go out of scope with no references, all data is freed for that Realm. It is recommended that you hold onto a strong reference to any created in-memory Realms for the duration of your app.
Error Handling
Like any disk IO operation, creating a RLMRealm
instance could sometimes fail if resources are constrained. In practice, this can only happen the first time a Realm instance is created on a given thread. Subsequent accesses to a Realm from the same thread will reuse a cached instance and will always succeed.
To handle errors when first accessing a Realm on a given thread, provide an NSError
pointer to the error
parameter:
NSError *error = nil;
RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
RLMRealm *realm = [RLMRealm realmWithConfiguration:config error:&error];
if (!realm) {
// handle error
}
Copying Objects Between Realms
Copying Realm objects to other Realms is as simple as passing in the original object to +[RLMObject createInRealm:withValue:]
. For example, [MyRLMObjectSubclass createInRealm:otherRealm withValue:originalObjectInstance]
. Remember that Realm objects can only be accessed from the thread on which they were first created, so this copy will only work for Realms on the same thread.
Finding a Realm File
If you need help finding your app’s Realm file, check this StackOverflow answer for detailed instructions.
Auxiliary Realm Files
Alongside the standard .realm
files, Realm also generates and maintains additional files and directories for its own internal operations.
.realm.lock
- A lock file for resource locks..realm.management
- Directory of interprocess lock files..realm.note
- A named pipe for notifications.
These files don’t have any effect on .realm
database files, and won’t cause any erroneous behavior if their parent database file is deleted or replaced.
When reporting Realm issues, please be sure to include these auxiliary files along with your main .realm
file as they contain useful information for debugging purposes.
Bundling a Realm with an App
It’s common to seed an app with initial data, making it available to your users immediately on first launch. Here’s how to do this:
- First, populate the Realm. You should use the same data model as your final, shipping app to create a Realm and populate it with the data you wish to bundle with your app. Since Realm files are cross-platform, you can use an OS X app (see our JSONImport example) or your iOS app running in the simulator.
- In the code where you’re generating this Realm file, you should finish by making a compacted copy of the file (see
-[RLMRealm writeCopyToPath:error:]
). This will reduce the Realm’s file size, making your final app lighter to download for your users. - Drag the new compacted copy of your Realm file to your final app’s Xcode Project Navigator.
- Go to your app target’s build phases tab in Xcode and add the Realm file to the “Copy Bundle Resources” build phase.
- At this point, your bundled Realm file will be accessible to your app. You can find its path by using
[[NSBundle mainBundle] pathForResource:ofType:]
. - If the bundled Realm contains fixed data that you don’t need to modify, you can open it directly from the bundle path by setting
readOnly = true
on theRLMRealmConfiguration
object. Otherwise, if it’s initial data that you’ll be modifying, you can copy the bundled file to your application’s Documents directory using[[NSFileManager defaultManager] copyItemAtPath:toPath:error:]
.
You can refer to our migration sample app for an example of how to use a bundled Realm file.
Class Subsets
In some scenarios you may wish to limit which classes can be stored in a specific Realm. For example, if you have two teams working on different components of your application which both use Realm internally, you may not want to have to coordinate migrations between them. You can do this by setting the objectClasses
property of your RLMRealmConfiguration
:
RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
config.objectClasses = @[MyClass.class, MyOtherClass.class];
RLMRealm *realm = [RLMRealm realmWithConfiguration:config error:nil];
Deleting Realm Files
In some cases, such as clearing caches, or resetting your entire dataset, it may be appropriate to completely delete a Realm file from disk.
Unlike most files, Realm files are memory-mapped and RLMRealm
instances expect the files to be available for the duration of the instance’s lifetime.
So while most files can be deleted by calling NSFileManager
’s removeItemAtPath
method you must take additional steps to ensure there are no active references to any RLMRealm
objects accessing the database file at the time of deletion.
RLMRealm
instances hold a database connection for the exact duration of their lifetime. One way to limit a Realm instance’s lifetime is to wrap its usage in an autorelease pool.
So all RLMRealm
instances with their fileURL
set to the Realm file you want to delete must be released before the deletion is performed.
Finally, although not strictly necessary, you should delete auxiliary Realm files as well as the main Realm file to fully clean up all related files.
@autoreleasepool {
// all Realm usage here
}
NSFileManager *manager = [NSFileManager defaultManager];
RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
NSArray<NSURL *> *realmFileURLs = @[
config.fileURL,
[config.fileURL URLByAppendingPathExtension:@"lock"],
[config.fileURL URLByAppendingPathExtension:@"log_a"],
[config.fileURL URLByAppendingPathExtension:@"log_b"],
[config.fileURL URLByAppendingPathExtension:@"note"]
];
for (NSURL *URL in realmFileURLs) {
NSError *error = nil;
[manager removeItemAtURL:URL error:&error];
if (error) {
// handle error
}
}
Using Realm with Background App Refresh
On iOS 8 and above, files inside apps are automatically encrypted using NSFileProtection
whenever the device is locked. If your app attempts to do any work involving Realm while the device is locked and the NSFileProtection
attributes of your Realm files are set to encrypt them (which is the case by default), an open() failed: Operation not permitted
exception will be thrown.
In order to handle this, it is necessary to ensure that the file protection attributes applied to both the Realm file itself and its auxiliary files is downgraded to a less strict one that allows file access even when the device is locked, such as NSFileProtectionCompleteUntilFirstUserAuthentication
.
If you choose to opt out of complete iOS file encryption in this way, we encourage you to use Realm’s own built-in encryption to ensure your data is still properly secured.
Since the auxiliary files can sometimes be lazily created and deleted mid-operation, we recommend that you apply the file protection attributes to the parent folder containing these Realm files. This will ensure the attribute is properly applied to all of the relevant Realm files, regardless of their creation time.
RLMRealm *realm = [RLMRealm defaultRealm];
// Get our Realm file's parent directory
NSString *folderPath = realm.configuration.fileURL.URLByDeletingLastPathComponent.path;
// Disable file protection for this directory
[[NSFileManager defaultManager] setAttributes:@{NSFileProtectionKey: NSFileProtectionNone}
ofItemAtPath:folderPath error:nil];
Threading
Within individual threads you can just treat everything as regular objects without worrying about concurrency or multithreading. There is no need for any locks or resource coordination to access them (even if they are simultaneously being modified on other threads) and it is only modifying operations that have to be wrapped in write transactions.
Realm makes concurrent usage easy by ensuring that each thread always has a consistent view of the Realm. You can have any number of threads working on the same Realms in parallel, and since they all have their own snapshots, they will never cause each other to see inconsistent state.
The only thing you have to be aware of is that you cannot have multiple threads sharing the same instances of Realm objects. If multiple threads need to access the same objects they will each need to get their own instances (otherwise changes happening on one thread could cause other threads to see incomplete or inconsistent data).
Seeing Changes From Other Threads
On the main UI thread (or any thread with a runloop) objects will automatically update with changes from other threads between each iteration of the runloop. At any other time you will be working on the snapshot, so individual methods always see a consistent view and never have to worry about what happens on other threads.
When you initially open a Realm on a thread, its state will be based off the most recent successful write commit, and it will remain on that version until refreshed. Realms are automatically refreshed at the start of every runloop iteration, unless RLMRealm’s autorefresh
property is set to NO
. If a thread has no runloop (which is generally the case in a background thread), then -[RLMRealm refresh]
must be called manually in order to advance the transaction to the most recent state.
Realms are also refreshed when write transactions are committed (-[RLMRealm commitWriteTransaction]
).
Failing to refresh Realms on a regular basis could lead to some transaction versions becoming “pinned”, preventing Realm from reusing the disk space used by that version, leading to larger file sizes. Refer to our Current Limitations for more details on this effect.
Passing Instances Across Threads
Unmanaged instances of RLMObject
s behave exactly as regular NSObject
subclasses, and are safe to pass across threads.
Instances of RLMRealm
, RLMResults
, or RLMArray
, or managed instances of RLMObject
are thread-confined, meaning that they can only be used on the thread on which they were created, otherwise an exception is thrown*. This is one way Realm enforces transaction version isolation. Otherwise, it would be impossible to determine what should be done when an object is passed between threads at different transaction versions without a potentially extensive relationship graph.
Realm exposes a mechanism to safely pass thread-confined instances in three steps:
- Initialize a
RLMThreadSafeReference
with the thread-confined object. - Pass that
RLMThreadSafeReference
to a destination thread or queue. - Resolve this reference on the target Realm by calling
-[RLMRealm resolveThreadSafeReference:]
. Use the returned object as you normally would.
For example:
Person *person = [Person new];
person.name = @"Jane";
[realm transactionWithBlock:^{
[realm addObject:person];
}];
RLMThreadSafeReference *personRef = [RLMThreadSafeReference
referenceWithThreadConfined:person];
dispatch_async(queue, ^{
RLMRealm *realm = [RLMRealm realmWithConfiguration:realm.configuration
error:nil];
Person *person = [realm resolveThreadSafeReference:personRef];
if (!person) {
return; // person was deleted
}
[realm transactionWithBlock:^{
person.name = @"Jane Doe";
}];
});
A RLMThreadSafeReference
object must be resolved at most once. Failing to resolve a RLMThreadSafeReference
will result in the source version of the Realm being pinned until the reference is deallocated. For this reason, RLMThreadSafeReference
should be short-lived.
* Some properties and methods on these types can be accessed from any thread:
RLMRealm
: all properties, class methods, and initializers.RLMObject
:isInvalidated
,objectSchema
,realm
, class methods, and initializers.RLMResults
:objectClassName
andrealm
.RLMArray
:isInvalidated
,objectClassName
, andrealm
.
Using a Realm Across Threads
To access the same Realm file from different threads, you must initialize a new Realm to get a different instance for every thread of your app. As long as you specify the same configuration, all Realm instances will map to the same file on disk.
Sharing Realm instances across threads is not supported. Realm instances accessing the same Realm file must also all use the same RLMRealmConfiguration
.
Realm can be very efficient when writing large amounts of data by batching together multiple writes within a single transaction. Transactions can also be performed in the background using Grand Central Dispatch to avoid blocking the main thread. RLMRealm
objects are not thread safe and cannot be shared across threads, so you must get a Realm instance in each thread/dispatch queue in which you want to read or write. Here’s an example of inserting a million objects in a background queue:
dispatch_async(queue, ^{
@autoreleasepool {
// Get realm and table instances for this thread
RLMRealm *realm = [RLMRealm defaultRealm];
// Break up the writing blocks into smaller portions
// by starting a new transaction
for (NSInteger idx1 = 0; idx1 < 1000; idx1++) {
[realm beginWriteTransaction];
// Add row via dictionary. Property order is ignored.
for (NSInteger idx2 = 0; idx2 < 1000; idx2++) {
[Person createInRealm:realm
withValue:@{@"name" : randomString,
@"birthdate" : randomDate}];
}
// Commit the write transaction
// to make this data available to other threads
[realm commitWriteTransaction];
}
}
});
JSON
Realm does not have direct support for JSON, but it’s possible to add RLMObject
s from JSON using the output of [NSJSONSerialization JSONObjectWithData:options:error:]
. The resulting KVC-compliant object can be used to add/update RLMObject
s using the standard APIs for creating and updating objects.
// A Realm Object that represents a city
@interface City : RLMObject
@property NSString *name;
@property NSInteger cityId;
// other properties left out ...
@end
@implementation City
@end // None needed
NSData *data = [@"{\"name\": \"San Francisco\", \"cityId\": 123}" dataUsingEncoding: NSUTF8StringEncoding];
RLMRealm *realm = [RLMRealm defaultRealm];
// Insert from NSData containing JSON
[realm transactionWithBlock:^{
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
[City createOrUpdateInRealm:realm withValue:json];
}];
If there are nested objects or arrays in the JSON, these will be mapped automatically to to-one and to-many relationships - see the Nested Objects section for more detail.
When inserting or updating JSON data in a Realm using this approach, be aware that Realm expects the JSON property names and types to exactly match the RLMObject properties. For example:
float
properties should be initialized withfloat
-backedNSNumbers
.NSDate
andNSData
properties cannot be automatically inferred from strings, but should be converted to the appropriate type before passing to[RLMObject createOrUpdateInRealm:withValue:]
.- If a JSON
null
(i.e.NSNull
) is supplied for a required property, an exception will be thrown. - If no property is supplied on insert for a required property, an exception will be thrown.
- Realm will ignore any properties in the JSON not defined by the
RLMObject
.
If your JSON schema doesn’t align exactly with your Realm objects, we recommend you use a third party model mapping framework in order to transform your JSON. Objective‑C has a thriving set of actively maintained model mapping frameworks which work with Realm, some of which are listed in the realm-cocoa repository.
Notifications
It is possible to register to be notified whenever an RLMRealm
, RLMResults
, RLMArray
or RLMLinkingObjects
is updated by calling the addNotificationBlock
method.
It’s also possible to observe changes to a single RLMObject
through Key-Value Observation.
When notifications can’t be delivered instantly, multiple write transactions may be coalesced into a single notification.
Notifications are delivered as long as a reference is held to the returned notification token. You should keep a strong reference to this token on the class registering for updates, as notifications are automatically unregistered when the notification token is deallocated.
Realm Notifications
Realm instances send out notifications to other instances on other threads every time a write transaction is committed:
// Observe Realm Notifications
token = [realm addNotificationBlock:^(NSString *notification, RLMRealm * realm) {
[myViewController updateUI];
}];
// later
[token stop];
Collection Notifications
Collection notifications are different from Realm notifications in that they contain information that describe what changes have occurred at a fine-grained level. This consists of the indices of objects that have been inserted, deleted, or modified since the last notification.
Collection notifications are delivered asynchronously, first with the initial results, and then again after each write transaction which changes either any of the objects in the collection, or which objects are in the collection.
These changes can be accessed via the RLMCollectionChange
parameter that is passed to the notification block. This object holds information about the indices affected by deletions
, insertions
and modifications
.
The former two, deletions and insertions, record the indices whenever objects start and stop being part of the collection. This takes into account when you add objects to the Realm or delete them from the Realm. For RLMResults
this also applies when you filter for specific values and the object was changed so that it is now matching the query or not matching anymore. For collections based either on RLMArray
or RLMLinkingObjects
, including derived RLMResults
, this applies in addition when objects are added or removed from the relationship.
You’re notified about modifications whenever a property of an object has changed, which was previously part of the collection and is still part of it. This happens as well when to-one and to-many relationships change, but doesn’t take changes on inverse relationships into account.
@interface Dog : RLMObject
@property NSString *name;
@property NSData *picture;
@property NSInteger age;
@end
@implementation Dog
@end
RLM_ARRAY_TYPE(Dog)
@interface Person : RLMObject
@property NSString *name;
@property RLMArray<Dog *><Dog> *dogs;
@end
@implementation Person
@end
So let’s assume you’re observing a list of dog owners as given by the model code above, you will be notified about modifications for a matched Person
object for example when:
- You modify the
Person
’sname
property. - You add or remove a
Dog
to thePerson
’sdogs
property. - You modify the
age
property of aDog
belonging to thatPerson
.
This makes it possible to discretely control the animations and visual updates made to the content inside your UI, instead of arbitrarily reloading everything each time a notification occurs.
- (void)viewDidLoad {
[super viewDidLoad];
// Observe RLMResults Notifications
__weak typeof(self) weakSelf = self;
self.notificationToken = [[Person objectsWhere:@"age > 5"] addNotificationBlock:^(RLMResults<Person *> *results, RLMCollectionChange *changes, NSError *error) {
if (error) {
NSLog(@"Failed to open Realm on background worker: %@", error);
return;
}
UITableView *tableView = weakSelf.tableView;
// Initial run of the query will pass nil for the change information
if (!changes) {
[tableView reloadData];
return;
}
// Query results have changed, so apply them to the UITableView
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[changes deletionsInSection:0]
withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView insertRowsAtIndexPaths:[changes insertionsInSection:0]
withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView reloadRowsAtIndexPaths:[changes modificationsInSection:0]
withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView endUpdates];
}];
}
- (void)dealloc {
[self.notificationToken stop];
}
Interface-Driven Writes
Notifications in Realm are always delivered asynchronously so your app never stutters or blocks the main UI thread. However, there are situations when changes need to be done synchronously, on the main thread, and reflected in the UI instantly. We refer to these transactions as interface-driven writes.
For example, say a user adds an item to a table view. The UI should ideally animate this operation and start this process as soon as the user initiates the action.
However, when the Realm change notification for this insertion is delivered a little later, it will indicate that an object was added to the collection backing the table view and we will once again attempt to insert a new row in the UI. This double insertion leads to inconsistent state between the UI and the backing data, which in turn will crash your app! 💥NSInternalInconsistencyException
💥
When performing an interface-driven write, pass the notification tokens of the notification blocks that shouldn’t react to a change for a second time to -[RLMRealm commitWriteTransactionWithoutNotifying:error:]Realm.commitWrite(withoutNotifying:)
.
This feature is especially useful when using fine-grained collection notifications with a synchronized Realm, because many of the workarounds to previously account for interface-driven writes rely on controlling the full state of when the app can perform changes. With synchronized Realms, changes are applied whenever they’re synchronized, which can happen at any point in the app’s lifetime.
// Observe RLMResults Notifications
__weak typeof(self) weakSelf = self;
self.notificationToken = [self.collection addNotificationBlock:^(RLMResults<Item *> *results, RLMCollectionChange *changes, NSError *error) {
if (error) {
NSLog(@"Failed to open Realm on background worker: %@", error);
return;
}
UITableView *tableView = weakSelf.tableView;
// Initial run of the query will pass nil for the change information
if (!changes) {
[tableView reloadData];
return;
}
// Query results have changed, so apply them to the UITableView
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[changes deletionsInSection:0]
withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView insertRowsAtIndexPaths:[changes insertionsInSection:0]
withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView reloadRowsAtIndexPaths:[changes modificationsInSection:0]
withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView endUpdates];
}];
- (void)insertItem {
// Perform an interface-driven write on the main thread:
[self.collection.realm beginWriteTransaction];
[self.collection insertObject:[Item new] atIndex:0];
// And mirror it instantly in the UI
[tableView insertRowsAtIndexPaths:[NSIndexPath indexPathForRow:0 inSection:0]
withRowAnimation:UITableViewRowAnimationAutomatic];
// Making sure the change notification doesn't apply the change a second time
[self.collection.realm commitWriteTransactionWithoutNotifying:@[token]];
}
Notification APIs
See the following API documentation for details:
-[RLMRealm addNotificationBlock:]
-[RLMCollection addNotificationBlock:]
-[RLMResults addNotificationBlock:]
-[RLMArray addNotificationBlock:]
-[RLMLinkingObjects addNotificationBlock:]
-[RLMNotificationToken stop]
Key-Value Observation
Realm objects are Key-Value Observing compliant for most properties. Almost all managed (non-ignored) properties on your RLMObject
subclasses are KVO-compliant, along with the invalidated
property on RLMObject
and RLMArray
. (RLMLinkingObjects
properties can’t be observed using KVO.)
Observing properties of unmanaged instances of RLMObject
subclasses works just like with any other NSObject
subclass, but note that you cannot add an object to a Realm (with [realm addObject:obj]
or other similar methods) while it has any registered observers.
Observing properties of managed objects (those previously added to a Realm) works a little differently. With managed objects, there are three times when the value of a property may change: when you directly assign to it; when you call [realm refresh]
or the Realm is automatically refreshed after a write transaction is committed on a different thread; and when you call [realm beginWriteTransaction]
after changes on a different thread which have not been picked up by a refresh on the current thread.
In the latter two cases, all of the changes made in the write transaction(s) on another thread will be applied at once, and KVO notifications will all be sent at once. Any intermediate steps are discarded, so if in the write transaction you incremented a property from one to ten, on the main thread you’ll get a single notification of a change directly from one to ten. Because properties can change in value when not in a write transaction or even as part of beginning a write transaction, trying to modify managed Realm objects from within -observeValueForKeyPath:ofObject:change:context:
is not recommended.
Unlike NSMutableArray
properties, observing changes made to RLMArray
properties does not require using -mutableArrayValueForKey:
, although that is supported for compatibility with code not written with Realm in mind. Instead, you can simply call the modification methods on RLMArray
directly, and anyone observing the property it is stored in will be notified.
In our example apps you can find a short example of using Realm with ReactiveCocoa from Objective‑C, and ReactKit from Swift.
Migrations
When working with any database, it is likely your data model will change over time. Since data models in Realm are defined as standard Objective‑C classes, making model changes is as easy as changing any other Objective‑C class. For example, suppose we have the following Person
model:
@interface Person : RLMObject
@property NSString *firstName;
@property NSString *lastName;
@property int age;
@end
We want to update the data model to require a fullName
property, rather than separate first and last names. To do this, we simply change the object interface to the following:
@interface Person : RLMObject
@property NSString *fullName;
@property int age;
@end
At this point if you had saved any data with the previous model version, there will be a mismatch between what Realm sees defined in code and the data Realm sees on disk. When this occurs, an exception will be thrown when you try to open the existing file unless you run a migration.
Note that default property values aren’t applied to new objects or new properties on existing objects during migrations. We consider this to be a bug and are tracking it as #1793.
Performing a Migration
Defining Migrations
You define a migration and the associated schema version by setting RLMRealmConfiguration.schemaVersion
and RLMRealmConfiguration.migrationBlock
. Your migration block provides all the logic for converting data models from previous schemas to the new schema. When creating a RLMRealm
with this configuration, the migration block will be applied to update the RLMRealm
to the given schema version if a migration is needed.
For example, suppose we want to migrate the Person
model declared earlier. The minimal necessary migration block would be the following:
// Inside your [AppDelegate didFinishLaunchingWithOptions:]
RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
// Set the new schema version. This must be greater than the previously used
// version (if you've never set a schema version before, the version is 0).
config.schemaVersion = 1;
// Set the block which will be called automatically when opening a Realm with a
// schema version lower than the one set above
config.migrationBlock = ^(RLMMigration *migration, uint64_t oldSchemaVersion) {
// We haven’t migrated anything yet, so oldSchemaVersion == 0
if (oldSchemaVersion < 1) {
// Nothing to do!
// Realm will automatically detect new properties and removed properties
// And will update the schema on disk automatically
}
};
// Tell Realm to use this new configuration object for the default Realm
[RLMRealmConfiguration setDefaultConfiguration:config];
// Now that we've told Realm how to handle the schema change, opening the file
// will automatically perform the migration
[RLMRealm defaultRealm];
At the very minimum all we need to do is to update the version with an empty block to indicate that the schema has been upgraded (automatically) by Realm.
Updating Values
While this is the minimum acceptable migration, we probably want to use this block to populate any new properties (in this case fullName
) with something meaningful. Within the migration block we can call [RLMMigration enumerateObjects:block:]
to enumerate each RLMObject
of a certain type, and apply any necessary migration logic. Notice how for each enumeration the existing RLMObject
instance is accessed via an oldObject
variable and the updated instance is accessed via newObject
:
// Inside your [AppDelegate didFinishLaunchingWithOptions:]
RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
config.schemaVersion = 1;
config.migrationBlock = ^(RLMMigration *migration, uint64_t oldSchemaVersion) {
// We haven’t migrated anything yet, so oldSchemaVersion == 0
if (oldSchemaVersion < 1) {
// The enumerateObjects:block: method iterates
// over every 'Person' object stored in the Realm file
[migration enumerateObjects:Person.className
block:^(RLMObject *oldObject, RLMObject *newObject) {
// combine name fields into a single field
newObject[@"fullName"] = [NSString stringWithFormat:@"%@ %@",
oldObject[@"firstName"],
oldObject[@"lastName"]];
}];
}
};
[RLMRealmConfiguration setDefaultConfiguration:config];
Once the migration is successfully completed, the Realm and all of its objects can be accessed as usual by your app.
Renaming Properties
Renaming the property on a class as part of a migration is more efficient than copying values and preserves relationships rather than duplicating them.
To rename a property during a migration, make sure that your new models have a property with the new name and don’t have a property with the old name.
If the new property has different nullability or indexing settings, those will be applied during the rename operation.
Here’s how you could rename Person
’s yearsSinceBirth
property to age
:
// Inside your [AppDelegate didFinishLaunchingWithOptions:]
RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
config.schemaVersion = 1;
config.migrationBlock = ^(RLMMigration *migration, uint64_t oldSchemaVersion) {
// We haven’t migrated anything yet, so oldSchemaVersion == 0
if (oldSchemaVersion < 1) {
// The renaming operation should be done outside of calls to `enumerateObjects:`.
[migration renamePropertyForClass:Person.className oldName:@"yearsSinceBirth" newName:@"age"];
}
};
[RLMRealmConfiguration setDefaultConfiguration:config];
Adding more versions
Suppose now we have two previous versions of the Person
class:
// v0
// @interface Person : RLMObject
// @property NSString *firstName;
// @property NSString *lastName;
// @property int age;
// @end
// v1
// @interface Person : RLMObject
// @property NSString *fullName; // new property
// @property int age;
// @end
// v2
@interface Person : RLMObject
@property NSString *fullName;
@property NSString *email; // new property
@property int age;
@end
The logic in our migration block might look like the following:
RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
config.schemaVersion = 2;
config.migrationBlock = ^(RLMMigration *migration, uint64_t oldSchemaVersion) {
// The enumerateObjects:block: method iterates
// over every 'Person' object stored in the Realm file
[migration enumerateObjects:Person.className
block:^(RLMObject *oldObject, RLMObject *newObject) {
// Add the 'fullName' property only to Realms with a schema version of 0
if (oldSchemaVersion < 1) {
newObject[@"fullName"] = [NSString stringWithFormat:@"%@ %@",
oldObject[@"firstName"],
oldObject[@"lastName"]];
}
// Add the 'email' property to Realms with a schema version of 0 or 1
if (oldSchemaVersion < 2) {
newObject[@"email"] = @"";
}
}];
};
[RLMRealmConfiguration setDefaultConfiguration:config];
// now that we have updated the schema version and provided a migration block,
// opening an outdated Realm will automatically perform the migration and
// opening the Realm will succeed
[RLMRealm defaultRealm];
For a more complete look at the implementation of a data schema migration, check out our migration sample app.
Linear Migrations
Suppose we have two users for our app: JP and Tim. JP updates the app very often, but Tim happens to skip a few versions. It’s likely that JP has seen every new version of our app, and every schema upgrade in order: he downloaded a version of the app that took him from v0 to v1, and later another update that took him from v1 to v2. In contrast, it’s possible that Tim might download an update of the app that will need to take him from v0 to v2 immediately. Structuring your migration blocks with non-nested if (oldSchemaVersion < X)
calls ensures that they will see all necessary upgrades, no matter which schema version they start from.
Another scenario may arise in the case of users who skipped versions of your app. If you delete a property email
at version 2 and re-introduce it at version 3, and a user jumps from version 1 to version 3, Realm will not be able to automatically detect the deletion of the email
property, as there will be no mismatch between the schema on disk and the schema in the code for that property. This will lead to Tim’s Person object having a v3 address property that has the contents of the v1 address property. This may not be a problem, unless you changed the internal storage representation of that property between v1 and v3 (say, went from an ISO address representation to a custom one). To avoid this, we recommend you nil out the email
property on the if (oldSchemaVersion < 3)
statement, guaranteeing that all Realms upgraded to version 3 will have a correct dataset.
Encryption
Realm’s encryption APIs are available for iOS, OS X & WatchKit but not watchOS because the <mach/mach.h>
and <mach/exc.h>
APIs used by Realm’s encryption mechanism are marked as __WATCHOS_PROHIBITED
.
We’ve filed a radar about this issue: rdar://22063654.
Please take note of the Export Compliance section of our LICENSE, as it places restrictions against the usage of Realm if you are located in countries with an export restriction or embargo from the United States.
Realm supports encrypting the database file on disk with AES-256+SHA2 by supplying a 64-byte encryption key when creating a Realm.
// Generate a random encryption key
NSMutableData *key = [NSMutableData dataWithLength:64];
(void)SecRandomCopyBytes(kSecRandomDefault, key.length, (uint8_t *)key.mutableBytes);
// Open the encrypted Realm file
RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
config.encryptionKey = key;
NSError *error = nil;
RLMRealm *realm = [RLMRealm realmWithConfiguration:config error:&error];
if (!realm) {
// If the encryption key is wrong, `error` will say that it's an invalid database
NSLog(@"Error opening realm: %@", error);
}
// Use the Realm as normal
RLMResults<Dog *> *dogs = [Dog objectsInRealm:realm where:@"name contains 'Fido'"];
This makes it so that all of the data stored on disk is transparently encrypted and decrypted with AES-256 as needed, and verified with a SHA-2 HMAC. The same encryption key must be supplied every time you obtain a Realm instance.
See our encryption sample app for an end-to-end app that generates an encryption key, stores it securely in the keychain, and uses it to encrypt a Realm.
There is a small performance hit (typically less than 10% slower) when using encrypted Realms.
Testing and Debugging
Configuring the Default Realm
The easiest way to use and test Realm apps is to use the default Realm. To avoid overriding application data or leaking state between tests, you can simply set the default Realm to a new file for each test.
// A base class which each of your Realm-using tests should inherit from rather
// than directly from XCTestCase
@interface TestCaseBase : XCTestCase
@end
@implementation TestCaseBase
- (void)setUp {
[super setUp];
// Use an in-memory Realm identified by the name of the current test.
// This ensures that each test can't accidentally access or modify the data
// from other tests or the application itself, and because they're in-memory,
// there's nothing that needs to be cleaned up.
RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
config.inMemoryIdentifier = self.name;
[RLMRealmConfiguration setDefaultConfiguration:config];
}
@end
Injecting Realm Instances
Another way to test Realm-related code is to have all the methods you’d like to test accept a RLMRealm
instance as an argument, so that you can pass in different Realms when running the app and when testing it. For example, suppose your app has a method to GET
a user profile from a JSON API and you’d like to test that the local profile is properly created:
// Application Code
@implementation ClassBeingTested
+ (void)updateUserFromServer {
NSURL *url = [NSURL URLWithString:@"http://myapi.example.com/user"];
[[[NSURLSession sharedSession] dataTaskWithURL:url
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error) {
[self createOrUpdateUserInRealm:[RLMRealm defaultRealm] withData:data];
}] resume];
}
+ (void)createOrUpdateUserInRealm:(RLMRealm *)realm withData:(NSData *)data {
id object = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingOptions)nil error:nil];
[realm transactionWithBlock:^{
[User createOrUpdateInRealm:realm withValue:object];
}];
}
@end
// Test Code
@implementation UnitTests
- (void)testThatUserIsUpdatedFromServer {
RLMRealm *testRealm = [RLMRealm realmWithURL:kTestRealmURL];
NSData *jsonData = [@"{\"email\": \"help@realm.io\"}"
dataUsingEncoding:NSUTF8StringEncoding];
[ClassBeingTested createOrUpdateUserInRealm:testRealm withData:jsonData];
User *expectedUser = [User new];
expectedUser.email = @"help@realm.io";
XCTAssertEqualObjects([User allObjectsInRealm:testRealm][0],
expectedUser,
@"User was not properly updated from server.");
}
@end
Debugging
Debugging your Realm apps is easy, with LLDB support and the ability to view your app’s data in the Realm Studio.
Our Xcode Plugin comes with a LLDB script which adds support for inspecting managed RLMObject
s, RLMResults
and RLMArray
s objects in Xcode’s UI, rather than just displaying every property as nil
or 0
:
N.B.: The script currently only supports Objective‑C. Swift support is in progress.
Avoid Linking Realm and Tested Code in Test Targets
If you’re using Realm as a dynamic framework, you’ll need to make sure your unit test target can find Realm. You can do this by adding the parent path to Realm.framework
to your unit test’s “Framework Search Paths”.
If your tests fail with an exception message "Object type 'YourObject' is not managed by the Realm"
, it’s likely because you’ve linked the Realm framework directly to your test target, which should not be done. Unlinking Realm from your test target should address that.
You should also make sure to only compile your model class files in your application or framework targets; never add them to your unit test targets. Otherwise, those classes will be duplicated when testing, which can lead to difficult to debug issues (see this issue for details).
You’ll need to make sure all the code you need to test is exposed to your unit test targets (use the public
access modifier or @testable
). See this Stack Overflow answer for details.
Current Limitations
Here’s a list of our most commonly hit limitations.
Please refer to our GitHub issues for a more comprehensive list of known issues.
General Limits
Realm aims to strike a balance between flexibility and performance. In order to accomplish this goal, realistic limits are imposed on various aspects of storing information in a Realm. For example:
- Class names are limited to a maximum of 57 UTF8 characters.
- Property names are limited to a maximum of 63 UTF8 characters.
NSData
andNSString
properties cannot hold data exceeding 16MB in size. To store larger amounts of data, either break it up into 16MB chunks or store it directly on the file system, storing paths to these files in the Realm. An exception will be thrown at runtime if your app attempts to store more than 16MB in a single property.- Any single Realm file cannot be larger than the amount of memory your application would be allowed to map in iOS — this changes per device, and depends on how fragmented the memory space is at that point in time (there is a radar open about this issue: rdar://17119975). If you need to store more data, you can map it over multiple Realm files.
- String sorting and case insensitive queries are only supported for character sets in ‘Latin Basic’, ‘Latin Supplement’, ‘Latin Extended A’, ‘Latin Extended B’ (UTF-8 range 0-591).
Threads
Although Realm files can be accessed by multiple threads concurrently, you cannot hand over Realms, Realm objects, queries, and results between threads. Read more about Realm’s threading.
Realm Object Setters & Getters cannot be overriden
Since Realm overrides setters and getters to back properties directly by the underlying database, you cannot override them on your objects. A simple workaround is to create new, Realm-ignored properties, whose accessors can be overriden, and can call other setters/getters.
File size & tracking of intermediate versions
You should expect a Realm database to take less space on disk than an equivalent SQLite database. If your Realm file is much larger than you expect, it may be because you have a RLMRealm
that is referring to an older version of the data in the database.
In order to give you a consistent view of your data, Realm only updates the active version accessed at the start of a run loop iteration. This means that if you read some data from the Realm and then block the thread on a long-running operation while writing to the Realm on other threads, the version is never updated and Realm has to hold on to intermediate versions of the data which you may not actually need, resulting in the file size growing with each write. The extra space will eventually be reused by future writes, or may be compacted — for example by calling writeCopyToPath:error:
.
To avoid this issue, you may call invalidate
to tell Realm that you no longer need any of the objects that you’ve read from the Realm so far, which frees us from tracking intermediate versions of those objects. The Realm will update to the latest version the next time it is accessed.
You may also see this problem when accessing Realm using Grand Central Dispatch. This can happen when a Realm ends up in a dispatch queue’s autorelease pool as those pools may not be drained for some time after executing your code. The intermediate versions of data in the Realm file cannot be reused until the RLMRealm
object is deallocated. To avoid this issue, you should use an explicit autorelease pool when accessing a Realm from a dispatch queue.
Realm doesn’t have auto-incrementing properties
Realm doesn’t have a mechanism for thread-/process-safe auto-incrementing properties commonly used in other databases when generating primary keys. However, in most situations where a unique auto-generated value is desired, it isn’t necessary to have sequential, contiguous, integer IDs.
In these cases, a unique string primary key is typically sufficient. A common pattern is to set the default property value to [[NSUUID UUID] UUIDString]
to generate unique string IDs.
Another common motivation for auto-incrementing properties is to preserve order of insertion. In some situations, this can be accomplished by appending objects to a RLMArray
or by using a createdAt
property with a default value of [NSDate date]
.
Encrypted Realms cannot be accessed by multiple processes simultaneously
This includes iOS Extensions. To work around this, use unencrypted Realms, which can be shared across processes. You can make use of the Security and CommonCrypto system frameworks to encrypt and decrypt data stored in NSData
properties on Realm objects.
We’re tracking lifting this limitation in both the Realm Cocoa issue tracker (#1693) and the Realm Core issue tracker (#1845).
Recipes
We’ve put together some recipes showing how to use Realm to accomplish a few specific tasks. We add more recipes regularly, so check back often. If there’s an example you’d like to see, please open an issue on GitHub.
- Simplifying RESTful APIs with Mantle & Realm
- Building an iOS Clustered Map View in Objective‑C
- Building an iOS Search Controller in Objective‑C
- Building a Grid Layout with UICollectionView and Realm in Objective‑C
Sync
The Realm Mobile Platform extends the capabilities of the Realm Mobile Database, enabling automatic synchronization of data across devices. To support this additional functionality, new types and methods are provided. These sync-specific APIs are a strictly additive extension to the Realm Mobile Platform API, and are covered below.
Users
The user is one of the fundamental components of the Realm Mobile Platform. Users are represented by the RLMSyncUser
type.
Every Realm configured for synchronization is owned by a user. To open a synced Realm, a logged-in user is required. In order to create and log in a user, two values are required:
- The URL of the Realm Object Server you wish to connect to.
- A
RLMSyncCredentials
value that uniquely identifies a user.
Server URL
The authentication server URL is simply a NSURL
representing the location of the Realm Object Server.
NSURL *serverURL = [NSURL URLWithString:@"http://my.realmServer.com:9080"];
For more details about the server URL, please refer to our authentication documentation.
Authentication
Authentication is used to establish the identity of users and log them in. Refer to our authentication documentation for a list of authentication providers supported by the Realm Mobile Platform.
The credential information for a given user is represented by a RLMSyncCredentials
value. Here are some examples of credentials configured for authentication with various providers:
RLMSyncCredentials *usernameCredentials = [RLMSyncCredentials credentialsWithUsername:@"username"
password:@"password"
register:NO];
RLMSyncCredentials *googleCredentials = [RLMSyncCredentials credentialsWithGoogleToken:@"Google token"];
RLMSyncCredentials *facebookCredentials = [RLMSyncCredentials credentialsWithFacebookToken:@"Facebook token"];
RLMSyncCredentials *cloudKitCredentials = [RLMSyncCredentials credentialsWithCloudKitToken:@"CloudKit token"];
Most of these credentials simply take an authentication token provided by that service. Your application is responsible for logging into the service and obtaining the token.
The username and password credentials type is unique because it is entirely managed by the Realm Server, giving you full control over your app’s user management. Its factory method also takes a boolean argument that indicates whether a new user should be registered, or an existing user logged in. It is an error to register a new user with the username of an existing user, or to log in a user that doesn’t exist.
Authenticating the User
To create a user, call +[RLMSyncUser logIn]
. This factory method will initialize the user, asynchronously log them in to the Realm Object Server, and then provide the user object for further use in a callback block if the login process is successful. If the login process fails for any reason, an error object will be passed into the callback instead.
[RLMSyncUser logInWithCredentials:usernameCredentials
authServerURL:serverURL
onCompletion:^(RLMSyncUser *user, NSError *error) {
if (user) {
// can now open a synchronized RLMRealm with this user
} else if (error) {
// handle error
}
}];
The Realm Mobile Platform allows an application to support multiple users at the same time.
For example, consider an email client app that supports connecting to multiple email accounts. Multiple users (one for each email account) can be authenticated at any given time, with all active users being available via +[RLMSyncUser all]
.
Logging Out
To log a user out of their account, call -[RLMSyncUser logOut]
. Any pending local changes will continue to be uploaded until the Realm Object Server has been fully synchronized. Then, all of their local synced Realms will be deleted from their device.
Opening a Synchronized Realm
Synced Realms are created using the same RLMRealmConfiguration
and factory methods that are used to create standalone Realms.
Synced Realms must set the syncConfiguration
property on their RLMRealmConfiguration
with a RLMSyncConfiguration
value. A RLMRealmConfiguration
, in turn, is constructed using a RLMSyncUser
and a Realm URL representing the location of the synced Realm on the Realm Object Server. This URL may contain the tilde character (~
), which will be transparently expanded to represent the user’s unique identifier.
For example, the following might be a Realm URL, representing a “Widgets” Realm that each user has a copy of: realms://acme.example.com/~/widgets
. One user might have the unique identifier 5917268
. Their Realm URL for the “Widgets” Realm will expand to: realms://acme.example.com/5917268/widgets
. Another user might have the unique identifier 8581230
. Their Realm URL will expand to: realms://acme.example.com/8581230/widgets
. This allows your application logic to work with a URL representing the current user’s copy of a Realm all users access to, without needing to worry about managing the unique expanded representation of URLs for each individual user.
The configuration values for a synced Realm cannot have an inMemoryIdentifier
or fileURL
configured. Setting either property will automatically nil out the syncConfiguration
property. The framework is responsible for managing how synchronized Realms are cached or stored on disk.
The following example shows how a synced Realm might be opened given a user object and a Realm URL:
RLMSyncUser *user;
// Create the configuration
NSURL *syncServerURL = [NSURL URLWithString: @"realm://localhost:9080/~/userRealm"];
RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
config.syncConfiguration = [[RLMSyncConfiguration alloc] initWithUser:user realmURL:syncServerURL];
// Open the remote Realm
RLMRealm *realm = [RLMRealm realmWithConfiguration:config error:nil];
// Any changes made to this Realm will be synced across all devices!
Sync Sessions
A synced Realm’s connection to the Realm Object Server is represented by a RLMSyncSession
object. Session objects representing Realms opened by a specific user can be retrieved from that user’s RLMSyncUser
object using the +[RLMSyncUser allSessions]
or -[RLMSyncUser sessionForURL:]
APIs.
Basic Operations
The state of the underlying session can be retrieved using the state
property. This can be used to check whether the session is active, not connected to the server, or in an error state.
If the session is valid, the configuration
property will contain a RLMSyncConfiguration
value that can be used to open another instance of the same Realm (for example, on a different thread).
Progress Notifications
Session objects allow your app to monitor the status of a session’s uploads to and downloads from the Realm Object Server by registering progress notification blocks on a session object.
Progress notification blocks will be invoked periodically by the synchronization subsystem on the runloop of the thread in which they were originally registered. If no runloop exists, one will be created. (Practically speaking, this means you can register these blocks on background queues in GCD and they will work fine.) As many blocks as needed can be registered on a session object simultaneously. Blocks can either be configured to report upload progress or download progress.
Each time a block is called, it will receive the current number of bytes already transferred, as well as the total number of transferrable bytes (defined as the number of bytes already transferred plus the number of bytes pending transfer).
When a block is registered, the registration method returns a token object. The -stop
method can be called on the token to stop notifications for that particular block. If the block has already been deregistered, calling the stop method does nothing.
There are two types of blocks. Blocks can be configured to report indefinitely. These blocks will remain active unless explicitly stopped by the user and will always report the most up-to-date number of transferrable bytes. This type of block could be used to control a network indicator UI that, for example, changes color or appears only when uploads or downloads are actively taking place.
RLMSyncSession *session = [[RLMSyncUser currentUser] sessionForURL:realmURL];
void(^workBlock)(NSUInteger, NSUInteger) = ^(NSUInteger downloaded, NSUInteger downloadable) {
if ((double)downloaded/(double)downloadable >= 1) {
[viewController hideActivityIndicator];
} else {
[viewController showActivityIndicator];
}
};
RLMProgressNotificationToken *token;
token = [session addProgressNotificationForDirection:RLMSyncProgressDirectionDownload
mode:RLMSyncProgressReportIndefinitely
block:workBlock];
Blocks can also be configured to report progress for currently outstanding work. These blocks capture the number of transferrable bytes at the moment they are registered and always report progress relative to that value. Once the number of transferred bytes reaches or exceeds that initial value, the block will automatically unregister itself. This type of block could, for example, be used to control a progress bar that tracks the progress of an initial download of a synced Realm when a user signs in, letting them know how long it is before their local copy is up-to-date.
RLMSyncSession *session = [[RLMSyncUser currentUser] sessionForURL:realmURL];
RLMProgressNotificationToken *token;
void(^workBlock)(NSUInteger, NSUInteger) = ^(NSUInteger uploaded, NSUInteger uploadable) {
double progress = ((double)uploaded/(double)uploadable);
progress = progress > 1 ? 1 : progress;
[viewController updateProgressBarWithFraction:progress];
if (progress == 1) {
[viewController hideProgressBar];
[token stop];
}
};
token = [session addProgressNotificationForDirection:RLMSyncProgressDirectionUpload
mode:RLMSyncProgressForCurrentlyOutstandingWork
block:workBlock];
Access Control
The Realm Mobile Platform provides flexible access control mechanisms to specify which users are allowed to sync against which Realm files. This can be used, for example, to create collaborative apps where multiple users write to the same Realm. It can also be used to share data in a publisher/subscriber scenario where a single writing user shares data with many users with read permissions.
There are three axes that control the access level (permissions) of a given Realm:
mayRead
indicates that the user is allowed to read from the Realm,mayWrite
indicates that the user is allowed to write to the Realm,mayManage
indicates that the user is allowed to change the permissions for the Realm.
Unless permissions are explicitly modified, only the owner of a Realm can access it. The only exception is administrator users: they are always granted all permissions to all Realms on the server.
Please refer to the general Realm Object Server documentation on Access Control to learn more about access control.
Management Realm
All access level management operations can be performed by writing to the Management Realm. The Management Realm can be written to or read from just like a regular synchronized Realm. However, the Realm Object Server is specifically designed to react to changes to this Realm. Permission Change objects can be added to this Realm to modify the access control settings of a Realm file.
To obtain the Management Realm for a given user, call the -[RLMSyncUser managementRealmWithError:]
method.
Modifying Permissions
To modify the access control settings for a Realm file, add a RLMSyncPermissionChange
object to the Management Realm.
[managementRealm transactionWithBlock:^{
RLMSyncPermissionChange *permissionChange = [RLMSyncPermissionChange permissionChangeWithRealmURL:realmURL // The remote Realm URL on which to apply the changes
userID:anotherUser // The user ID for which these permission changes should be applied
read:@YES // Grant read access
write:@YES // Grant write access
manage:@NO]; // Grant management access
[managementRealm addObject:permissionChange];
} error:&error];
To apply the permission changes for all Realms managed by the Sync User, specify a realmURL
value of *
. To apply the permission changes for all Sync Users authorized with the Object Server, specify a userID
value of *
.
Once the Object Server has processed the operations encoded in the RLMSyncPermissionChange
object, it will set that object’s status
and statusMessage
properties.
To be notified of the result of the permission change operation, simply observe the permission change object like any other Realm object (for example, using KVO or collection notifications). See Notifications for details.
The following example demonstrates how an application might be notified of the status of permission change operations:
token = [[RLMSyncPermissionChange objectsInRealm:managementRealm where:@"id = %@", permissionChange.id]
addNotificationBlock:^(RLMResults<RLMSyncPermissionChange *> *results, RLMCollectionChange *change, NSError *error) {
switch (results[0].status) {
case RLMSyncManagementObjectStatusNotProcessed:
break; // handle case
case RLMSyncManagementObjectStatusSuccess:
break; // handle case
case RLMSyncManagementObjectStatusError:
break; // handle case
}
NSLog(@"%@", results[0].statusMessage); // contains error or informational message
}];
Logging
The synchronization subsystem supports a number of logging levels, useful while developing an app. These can be selected by setting the logLevel
property on the RLMSyncManager
singleton to the desired verbosity:
[[RLMSyncManager sharedManager] setLogLevel:RLMSyncLogLevelOff];
The logging levels are described in the Realm Object Server configuration documentation.
Error reporting
Certain sync-related APIs perform asynchronous operations that may fail. These APIs take completion blocks which accept an error parameter; if the error parameter is passed in the operation failed. The error can be checked for more detail.
It is strongly recommended to also set an error handler on the RLMSyncManager
singleton. Errors involving the global synchronization subsystem or specific sessions (which represent Realms opened for synchronization with the server) will be reported through this error handler. When errors occur, the error handler will be called with an object representing the error, as well as a RLMSyncSession
object representing the session the error occurred on (if applicable).
[[RLMSyncManager sharedManager] setErrorHandler:^(NSError *error, RLMSyncSession *session) {
// handle error
}];
Errors
Realm Mobile Platform errors are represented by NSError
objects whose domain is RLMSyncErrorDomain
. Please consult the definitions of RLMSyncError
and RLMSyncAuthError
for error codes and what they mean.
Client Reset
If a Realm Object Server crashes and must be restored from a backup, there is a possibility that an app might need to carry out a client reset on a given synced Realm. This will happen if the local version of the Realm in question is greater than the version of that same Realm on the server (for example, if the application made changes after the Realm Object Server was backed up, but those changes weren’t synced back to the server before it crashed and had to be restored).
The client reset procedure is as follows: a backup copy of the local Realm file is made, and then the Realm files are deleted. The next time the app connects to the Realm Object Server and opens that Realm, a fresh copy will be downloaded. Changes that were made after the Realm Object Server was backed up but weren’t synced back to the server will be preserved in the backup copy of the Realm, but will not be present when the Realm is re-downloaded.
The need for a client reset is indicated by an error sent to the RLMSyncManager
error handler. This error will be denoted by the code RLMSyncErrorClientResetError
.
The error object will also contain two values: the location of the backup copy of the Realm file once the client reset process is carried out, and an opaque block that takes no arguments which can be called to initiate the client reset process.
If the block is called to manually initiate the client reset process, all instances of the Realm in question must first be invalidated and destroyed before the block is invoked. Note that a RLMRealm
might not be fully invalidated, even if all references to it are nil’ed out, until the autorelease pool containing it is drained. However, doing so allows the Realm to be immediately re-opened after the client reset process is complete, allowing syncing to resume.
If the block is not called, the client reset process will automatically take place after the next time the app is launched, upon first accessing the RLMSyncManager
singleton. It is the app’s responsibility to persist the location of the backup copy if needed, so that the backup copy can be found later.
A Realm which needs to be reset can still be written to and read from, but no subsequent changes will be synced to the server until the client reset is complete and the Realm is re-downloaded. It is extremely important that your application listen for client reset errors and, at the very least, make provisions to save user data created or modified after a client reset is triggered so it can later be written back to the re-downloaded copy of the Realm.
The backup copy file path can be obtained by calling -[NSError rlmSync_clientResetBackedUpRealmPath]
upon the NSError
object. It can also be extracted directly from the userInfo
dictionary using the kRLMSyncPathOfRealmBackupCopyKey
key.
The reset initiation block can be obtained by calling -[NSError rlmSync_clientResetBlock]
upon the NSError
object. It can also be extracted directly from the userInfo
dictionary using the kRLMSyncInitiateClientResetBlockKey
key.
The following example shows how the client reset APIs might be used to carry out a client reset:
[[RLMSyncManager sharedManager] setErrorHandler:^(NSError *error, RLMSyncSession *session) {
if (error.code == RLMSyncErrorClientResetError) {
[realmManager closeRealmSafely];
[realmManager saveBackupRealmPath:[error rlmSync_clientResetBackedUpRealmPath]];
[error rlmSync_clientResetBlock]();
return;
}
// Handle other errors...
}];
For more information about how the Realm Object Server handles client reset, please refer to our server documentation.
Migrations
Automatic migrations are supported for synced Realms, but the schema version number must be incremented when making a change. At present, only additive changes are supported, such as adding a class or property to an existing class. Custom migration blocks will never be invoked.
Conflict Resolution
Conflict resolution support is described within the Realm Object Server documentation.
FAQ
How big is the Realm library?
Once your app is built for release, Realm should only add around one megabyte to its size. The releases we distribute are significantly larger because they include support for the iOS, watchOS and tvOS simulators, some debug symbols, and bitcode, all of which are stripped by Xcode automatically when you build your app.
Is Realm open source?
Yes! Realm’s internal C++ storage engine and the language SDKs over it are entirely open source and licensed under Apache 2.0.
Realm also optionally includes a closed-source synchronization component, but that is not required to use Realm as an embedded database.
I see a network call to Mixpanel when I run my app, what is that?
Realm collects anonymous analytics when your app is run with a debugger attached, or when it runs in a simulator. These analytics are completely anonymous and help us improve the product by flagging which versions of Realm, iOS, OS X, or which language you target and which versions we can deprecate support for. This call does not run when your app is in production, or running on your user’s devices — only from inside your simulator or when a debugger is attached. You can see exactly what we collect and how we collect it, as well as the rationale for doing so, in our source code.
Troubleshooting
Crash Reporting
We encourage you to use a crash reporter in your application. Many Realm operations could potentially fail at runtime (like any other disk I/O), so collecting crash reports from your application will help identify areas where either you (or us) can improve error handling and fix crashing bugs.
Most commercial crash reporters have the option of collecting logs. We strongly encourage you to enable this feature. Realm logs metadata information (but no user data) when throwing exceptions and in irrecoverable situations, and these messages can help debug when things go wrong.
Reporting Realm Issues
If you’ve found an issue with Realm, please either file an issue on GitHub or email us at help@realm.io with as much information as possible for us to understand and reproduce your issue.
The following information is very useful to us:
- Goals.
- Expected results.
- Actual results.
- Steps to reproduce.
- Code sample that highlights the issue (full Xcode projects that we can compile ourselves are ideal).
- Version of Realm / Xcode / OS X.
- Version of involved dependency manager (CocoaPods / Carthage).
- Platform, OS version, and architecture on which the bug happens (e.g. 64-bit iOS 8.1).
- Crash logs and stack traces. See Crash Reporting above for details.
Reinstalling Via Dependency Managers
If you’ve installed Realm via CocoaPods or Carthage and you’re experiencing build errors, then it’s likely that you’re either using an unsupported version of that dependency manager, Realm’s integration into the project didn’t succeed, or part of your build tools have stale caches.
If that is the case, please try removing the folders the dependency manager created and installing again.
You can also try deleting derived data and cleaning the build folder in Xcode; this can fix issues caused by updating build tool versions or making changes to your project setup such as adding a new target, sharing dependencies across targets, etc.
To clean the build folder, hold down the ‘Option’ key while opening the ‘Product’ menu, then choose ‘Clean Build Folder…’. You can also type ‘Clean’ into the Xcode help search menu and select the ‘Clean Build Folder…’ menu item when it shows up in the search results.
CocoaPods
Realm can be installed via CocoaPods 0.39.0 or greater.
If you have troubles with your CocoaPods integration, it might help to reset the integration state. To achieve that simply run the following commands in Terminal out of your project directory:
pod cache clean Realm
pod cache clean RealmSwift
pod deintegrate || rm -rf Pods
pod install --verbose
rm -rf ~/Library/Developer/Xcode/DerivedData
You can also use cocoapods-deintegrate instead of deleting the Pods directory. With CocoaPods 1.0, this comes as preinstalled plugin. If you’re using an older version, you may consider installing it by gem install cocoapods-deintegrate
. You can run it by pod deintegrate
. That removes all traces of CocoaPods from your Xcode project.
Carthage
Realm can be installed via Carthage 0.9.2 or later.
To remove all Carthage-managed dependencies from your project, simply run the following commands in Terminal out of your project directory:
rm -rf Carthage
rm -rf ~/Library/Developer/Xcode/DerivedData
carthage update
Realm Core Binary Fails to Download
When building Realm, part of the process includes downloading the core library as a static binary and integrating it into the realm-cocoa
project.
It’s been reported that in certain instances, the core binary fails to download with the following error:
Downloading core failed. Please try again once you have an Internet connection.
This error can occur due to any of the following reasons:
- Your IP address range is from a region that is on the list of United States embargoes. In order to comply with U.S. law, Realm has not been made available in that region. For more information, please see our license.
- You are located in mainland China, and due to the country-wide firewall are not able to properly access CloudFlare or Amazon AWS S3 services at the moment. Please see this Realm-Cocoa Issue for more information.
- Amazon AWS S3 could be experiencing service issues. Please check the AWS Service Health Dashboard and try again later.