当我打电话时:
[RLMRealm realmWithPath:@"example.realm"]
它崩溃并记录:
由于未捕获的异常“RLMException”而终止应用程序,原因:“open() failed: Operation not allowed”
除了使用 default.realm 和 [RLMRealm defaultRealm] 之外,我如何创建特定的领域文件?我是否遗漏了文档中的某些内容?
您是对的,这是创建新领域文件的方法,如果您提供文件系统中可写位置的完整路径,它将起作用:
NSString *documentsDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *customRealmPath = [documentsDirectory stringByAppendingPathComponent:@"example.realm"];
RLMRealm *realm = [RLMRealm realmWithPath:customRealmPath];
编辑:更新为适用于设备和模拟器的路径
查看我的答案https://github.com/realm/realm-cocoa/issues/4221
如果您在捆绑包中使用领域文件,它将在设备中崩溃。需要指定只读
readOnly:Realm 是否为只读(只读文件必须为 true)。
let path = Bundle.main.url(forResource: "mydata", withExtension: "realm")!
let configuration = Realm.Configuration(fileURL: path, readOnly: true)
realm = try! Realm(configuration: configuration)