Search code examples
iosrealm

Realm realmWithPath method crashes on iOS


When I call:

[RLMRealm realmWithPath:@"example.realm"]

It crashes and logs:

Terminating app due to uncaught exception 'RLMException', reason: 'open() failed: Operation not permitted'

How do I create a specific realm file besides using default.realm and [RLMRealm defaultRealm]? Am I missing something from the documentation?


Solution

  • You are right that this is the way to create a new realm file, and if you provide a full path to a writable location in the file system, it will work:

    NSString *documentsDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *customRealmPath = [documentsDirectory stringByAppendingPathComponent:@"example.realm"];
    RLMRealm *realm = [RLMRealm realmWithPath:customRealmPath];
    

    EDIT: Updated to a path that works on devices as well as the simulator