-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Realm sometimes crashes when writing if device is locked. #1874
Comments
This actually seems to happen fairly often when doing background updates and is thus blocking us from auto-updating content for Apple Watch. |
In the meantime, you'll need to also remove file protection from the lock and log files. |
I am removing them from all the Realm files that I know of (.realm, .realm.lock, .realm.log, .realm.log_a, .realm.log_b and .realm.note). This doesn't seem to fix the issue entirely, however. I do this once every time the user signs in. |
@segiddins Is there any way for you guys to create the files so that they do not have file protection set at all? This is causing a lot of issues for me now that I've enabled background updates. I have no way of knowing when Realm creates the files and therefore there are always scenarios that will crash when the app tries to access those files when the device is locked. I would appreciate some help with this a lot! |
I can reproduce this issue almost all the time. Before I'm opening the Realm, I'm removing file protection from all the Realm files that exist (note that .log files do not exist at this point):
Then immediately afterwards when I try to open the Realm, I get:
Could you advise how to fix this without you adding support for completely removing file protection altogether. |
Does disabling file protection for the directory work? The log files are created as needed and deleted when the realm is fully closed, which doesn't play well with setting the file protection in advance. |
Setting file protection for the containing directory does appear to work. I wrote the following bad test code: NSError *err = nil;
[[NSFileManager defaultManager] setAttributes:@{NSFileProtectionKey: NSFileProtectionComplete} ofItemAtPath:[RLMRealm.defaultRealmPath stringByDeletingLastPathComponent] error:&err];
[[NSFileManager defaultManager] setAttributes:@{NSFileProtectionKey: NSFileProtectionNone} ofItemAtPath:RLMRealm.defaultRealmPath error:&err];
[[NSFileManager defaultManager] setAttributes:@{NSFileProtectionKey: NSFileProtectionNone} ofItemAtPath:[RLMRealm.defaultRealmPath stringByAppendingString:@".lock"] error:&err];
[[NSFileManager defaultManager] setAttributes:@{NSFileProtectionKey: NSFileProtectionNone} ofItemAtPath:[RLMRealm.defaultRealmPath stringByAppendingString:@".log"] error:&err];
[[NSFileManager defaultManager] setAttributes:@{NSFileProtectionKey: NSFileProtectionNone} ofItemAtPath:[RLMRealm.defaultRealmPath stringByAppendingString:@".log_a"] error:&err];
[[NSFileManager defaultManager] setAttributes:@{NSFileProtectionKey: NSFileProtectionNone} ofItemAtPath:[RLMRealm.defaultRealmPath stringByAppendingString:@".log_b"] error:&err];
[[NSFileManager defaultManager] setAttributes:@{NSFileProtectionKey: NSFileProtectionNone} ofItemAtPath:[RLMRealm.defaultRealmPath stringByAppendingString:@".note"] error:&err];
@autoreleasepool {
[RLMRealm defaultRealm];
}
UIBackgroundTaskIdentifier task = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:task];
}];
NSLog(@"ready");
__block dispatch_block_t block;
block = ^{
@autoreleasepool {
RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
[Person createInRealm:realm withValue:@[@"", @[]]];
[realm commitWriteTransaction];
NSLog(@"bg");
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), dispatch_get_main_queue(), block);
}
};
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), dispatch_get_main_queue(), block); which consistently throws the same exception when I lock the device with a passcode set. If I instead change If you need other files to have file protection enabled, I suggest putting the Realm files in a subdirectory. |
Thanks for the help. I'll start testing this today. |
It seems that removing file protection from the directory fixes this issue. I think you should document this as a "best practise" until a better solution is available. There does not appear to be any need to touch the individual Realm files residing in that directory so long as the directory itself has data protection set to NSFileProtectionNone or NSFileProtectionCompleteUntilFirstUserAuthentication. |
This seems to happen quite often when using Realm in response to background fetch request. I'm manually removing the file protection from the Realm files when first accessing it, but this does not seem to be sufficient to fix this issue.
Please consider having an option to completely opt-out of the platform-provided file protection, as it's completely unnecessary when using an encrypted Realm. I also have no way of knowing when the files are created by Realm, so I cannot respond to those by fixing the file protection attributes by hand.
Note that I'm just assuming this is caused by the default file protection attributes, as this only happens when the device is locked.
The text was updated successfully, but these errors were encountered: