Error : Error Domain=io.realm Code=2 "Unable to open a realm at path '/var/mobile/Containers/Data/Application/_/Documents/default.realm.lock': open() failed: Operation not permitted." UserInfo={Error Code=2, NSFilePath=/var/mobile/Containers/Data/Application/_/Documents/default.realm.lock, Underlying=open("/var/mobile/Containers/Data/Application/_/Documents/default.realm.lock") failed: Operation not permitted, NSLocalizedDescription=Unable to open a realm at path '/var/mobile/Containers/Data/Application/_/Documents/default.realm.lock': open() failed: Operation not permitted.}
We have already tried this
https://github.com/realm/realm-cocoa/issues/3308#issuecomment-195621569
Realm framework version: 3.7.6
iOS/OSX version: across versions
Is that path anonymized? The failing path is obviously not a valid one, so if it's accurate the problem is somehow related to getting the app's document directory.
Crashlytics says the app is never in the background, so it's probably not an NSFileProtection-related issue. The stack trace sort of hints at it being the first start of the app (due to the signup/login VCs), and if that's actually the case then it might somehow be related to backup/restore setting weird permissions? Alternatively, is it possible that the crashing devices are ones with MDM profiles imposing odd restrictions?
The path is something like this /var/mobile/Containers/Data/Application/xxxxxxxx/Documents/default.realm.lock
And yes, its on startup based on breadcrumbs reported.
This is not reported in our internal testing, but only in production, so pretty hard to tell if its only on devices running MDM or scenarios around backup/restore.
We are seeing the same issue on an enterprise app deployed via MDM. In our case i believe the app is being launched in the background due to location updates, then crashing in the same place mentioned in this ticket. Any help would be greatly appreciated.
We are seeing the same issue on about 10% of our users. We are thinking it's something to do with the Data Protection capability we have enabled.
We were able to solve this issue by creating a separate directory for Realm and setting the file permissions on that directory.
guard let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else {
fatalError("Could not access documents directory when setting up Realm")
}
let realmDirectoryPath = documentsDirectory.appendingPathComponent("Realm")
do {
// Disable file protection to allow Realm to be used during Background App Refresh.
// See https://realm.io/docs/swift/latest/#using-realm-with-background-app-refresh
try FileManager.default.createDirectory(atPath: realmDirectoryPath.path, withIntermediateDirectories: true, attributes: [.protectionKey: FileProtectionType.none])
} catch {
fatalError("Error creating directory: \(error.localizedDescription)")
}
let realmFile = realmDirectoryPath.appendingPathComponent("default.realm")
We then specify realmFile in the realm config.
This is partially documented here however the documentation really should stipulate that you have to set a custom path in a subdirectory because attempting to set file protection to .none on your app's Documents directory does nothing.
@litso Do you have a reproduce the above issue. My issue is the same. I would love to try the fix you mentioned. But I have been unable to reproduce this crash that some of my users are facing
@sravich it would happen under these circumstances. we were able to detect app being launched from location updates via logging https://github.com/realm/realm-cocoa/issues/5938#issuecomment-437974809
Closing this issue due to no recent activity. Please comment if you have additional information that may help reproduce this issue.
We are experiencing this issue on an app with a very large user base. It only effects 0.05% of our sessions, but it is our largest crash by far. It is not a user-facing crash
We have hypothesized that it is due to a combination of data privacy lockdown (File Protection) and Background Refresh.
The Realm documentation mentions a workaround, but we have not yet implemented it because we will have to migrate our existing databases to a new directory.
Most helpful comment
We were able to solve this issue by creating a separate directory for Realm and setting the file permissions on that directory.
We then specify
realmFilein the realm config.This is partially documented here however the documentation really should stipulate that you have to set a custom path in a subdirectory because attempting to set file protection to
.noneon your app's Documents directory does nothing.