{"action":"created","issue":{"url":"https://api.github.com/repos/realm/realm-cocoa/issues/3283","repository_url":"https://api.github.com/repos/realm/realm-cocoa","labels_url":"https://api.github.com/repos/realm/realm-cocoa/issues/3283/labels{/name}","comments_url":"https://api.github.com/repos/realm/realm-cocoa/issues/3283/comments","events_url":"https://api.github.com/repos/realm/realm-cocoa/issues/3283/events","html_url":"https://github.com/realm/realm-cocoa/issues/3283","id":138014318,"number":3283,"title":"Realm crash (EXC_BAD_ACCESS) when writing while device is Locked.","user":{"login":"chucktee","id":171176,"avatar_url":"https://avatars.githubusercontent.com/u/171176?v=3","gravatar_id":"","url":"https://api.github.com/users/chucktee","html_url":"https://github.com/chucktee","followers_url":"https://api.github.com/users/chucktee/followers","following_url":"https://api.github.com/users/chucktee/following{/other_user}","gists_url":"https://api.github.com/users/chucktee/gists{/gist_id}","starred_url":"https://api.github.com/users/chucktee/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chucktee/subscriptions","organizations_url":"https://api.github.com/users/chucktee/orgs","repos_url":"https://api.github.com/users/chucktee/repos","events_url":"https://api.github.com/users/chucktee/events{/privacy}","received_events_url":"https://api.github.com/users/chucktee/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-03-02T22:37:43Z","updated_at":"2016-03-03T15:51:59Z","closed_at":null,"body":"My application writes location updates into the default Realm on iOS, Objective-C, 9.2.1, Realm 0.98.3\r\n\r\nIf the app is in the foreground, or background from pressing the home button, writes work as expected.\r\n\r\nOnce the device is locked, either by pressing the lock button or the device self locking- the next write crashes at in: inline void SharedGroup::commit_and_continue_as_read() at line 966- do_commit(). \r\n\r\nI'm not using Realm encryption nor iOS disk encryption.\r\n \r\nThe app crashes on iPhone 6, iPad mini (1st gen) and iPad Pro (haven't tested others) and it does not crash in the Simulator set to Locked.\r\n\r\nI had this issue a few months ago and was able to resolve it by setting NSFileProtectionNone against all files in the Realm path at startup.  Now, I have to set NSFileProtectionNone before every write otherwise I see the crash.  This seems like a lot of overhead and I'm wondering if there is a better approach.\r\n\r\nThis is what I call to clear the protection:\r\n```\r\nNSString *pathToRealmFile = [RLMRealmConfiguration defaultConfiguration].path;\r\n    NSArray *allRealmRelatedFiles = @[\r\n                                      pathToRealmFile,\r\n                                      [pathToRealmFile stringByAppendingString:@\".lock\"],\r\n                                      [pathToRealmFile stringByAppendingString:@\".log\"],\r\n                                      [pathToRealmFile stringByAppendingString:@\".log_a\"],\r\n                                      [pathToRealmFile stringByAppendingString:@\".log_b\"]\r\n                                      ];\r\n    for (NSString *path in allRealmRelatedFiles) {\r\n        NSError *error = nil;\r\n        //\r\n        [[NSFileManager defaultManager] setAttributes:@{NSFileProtectionKey: NSFileProtectionNone} ofItemAtPath:path error:&error];\r\n        if (error) {\r\n            NSLog(@\"Error clearing protection on %@\", path);\r\n        }\r\n    }\r\n```\r\n\r\nAny ideas are much appreciated.\r\nChuck-"},"comment":{"url":"https://api.github.com/repos/realm/realm-cocoa/issues/comments/191823258","html_url":"https://github.com/realm/realm-cocoa/issues/3283#issuecomment-191823258","issue_url":"https://api.github.com/repos/realm/realm-cocoa/issues/3283","id":191823258,"user":{"login":"mrackwitz","id":1389011,"avatar_url":"https://avatars.githubusercontent.com/u/1389011?v=3","gravatar_id":"","url":"https://api.github.com/users/mrackwitz","html_url":"https://github.com/mrackwitz","followers_url":"https://api.github.com/users/mrackwitz/followers","following_url":"https://api.github.com/users/mrackwitz/following{/other_user}","gists_url":"https://api.github.com/users/mrackwitz/gists{/gist_id}","starred_url":"https://api.github.com/users/mrackwitz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mrackwitz/subscriptions","organizations_url":"https://api.github.com/users/mrackwitz/orgs","repos_url":"https://api.github.com/users/mrackwitz/repos","events_url":"https://api.github.com/users/mrackwitz/events{/privacy}","received_events_url":"https://api.github.com/users/mrackwitz/received_events","type":"User","site_admin":false},"created_at":"2016-03-03T15:51:59Z","updated_at":"2016-03-03T15:51:59Z","body":"The problem here is that the auxiliary files are not guaranteed to exist all the time. They might be deleted. If that happens and they are recreated, then you would need to set the file attributes again. But there are no hooks for that. As a workaround, I'd recommend to set the attribute for the directory where the Realm is stored. If there are other files in there, which would better make use of the encryption, then you can move the Realm files into another directory as a workaround by setting the default configuration's path."}}