kevinnguy
kevinnguy

Reputation: 454

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?

Upvotes: 2

Views: 2056

Answers (2)

onmyway133
onmyway133

Reputation: 48075

See my answer https://github.com/realm/realm-cocoa/issues/4221

If you use realm file in bundle, it will crash in the device. Need to specify readonly

readOnly: Whether the Realm is read-only (must be true for read-only files).

let path = Bundle.main.url(forResource: "mydata", withExtension: "realm")!
let configuration = Realm.Configuration(fileURL: path, readOnly: true)
realm = try! Realm(configuration: configuration)

Upvotes: 0

Gusutafu
Gusutafu

Reputation: 755

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

Upvotes: 7

Related Questions