【发布时间】:2017-11-06 19:46:43
【问题描述】:
在我的应用中,我正在后台同步联系人,当设备被锁定时,应用崩溃:
由于未捕获的异常“RLMException”而终止应用程序,原因: '/var/mobile/Containers/Data/Application/FF77C6DA-970E-4FDD-AB69-C10C20607243/Documents/MyApp/default.realm.lock: 无法在路径打开领域 '/var/mobile/Containers/Data/Application/FF77C6DA-970E-4FDD-AB69-C10C20607243/Documents/MyApp/default.realm.lock': open() 失败:不允许操作。'
我试过了:
创建一个新文件夹:
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/MyApp"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:YES attributes:nil error:&error]; //Create folder
然后在此文件夹中创建 .realm 文件,并将文件保护设置为无。连同 NSFilePosixPermissions
[[NSFileManager defaultManager] setAttributes:@{NSFileProtectionKey: NSFileProtectionNone,
NSFilePosixPermissions : [NSNumber numberWithShort:0777]
}
ofItemAtPath:[[[RLMRealm defaultRealm]configuration]fileURL].URLByDeletingLastPathComponent.path error:nil];
当设备在同步时被锁定时,应用仍然会崩溃。
关于如何解决这个问题的任何想法?
【问题讨论】:
-
在 Xcode 目标权利中,您是否打开了数据保护? developer.apple.com/library/content/documentation/IDEs/…
-
是的,已开启!
-
我关闭它后,没有发生崩溃。感谢@BradBrighton 提供的信息
-
不客气。我将在答案中发表评论,以便可以关闭问题。很高兴它有帮助!
-
@Teja Nandamuri 在你的场景中你能解释一下你是如何复制崩溃的吗?我看到崩溃,但我不知道要复制它们,请你帮忙。
标签: ios objective-c realm