Search code examples
iosrealm

iOS crash : Unable to open a realm at Path


I am seeing a number of crashes on Crashlytics but unable to reproduce the the crash which is happening on app launch. Does anyone know how to reproduce the below crash. I have tried all the answers on Github and stackoverflow and no one has mentioned about reproducing this specific crash. So if i do have a fix there is no way to test it except putting the fix in production and then assuming Crashlytics wont show crashes.

Please help.

Error:

Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=io.realm Code=2 "Unable to open a realm at path '/var/mobile/Containers/Data/Application/03BA7C37-9852-4A01-8EF6-949E308EADCD/Documents/default.realm.lock': open() failed: Operation not permitted." UserInfo={Error Code=2, NSFilePath=/var/mobile/Containers/Data/Application/03BA7C37-9852-4A01-8EF6-949E308EADCD/Documents/default.realm.lock,

calling code

//passing the fileProtectionType as .none
RealmHelper.configureRealmFiles(with: .none)

Sample code

   static func configureRealmFiles(with fileProtectionType: FileProtectionType) {
            guard let folderPath = realm.configuration.fileURL?.deletingLastPathComponent().path else {
                return
            }

            do {
                try FileManager.default.setAttributes([FileAttributeKey.protectionKey: fileProtectionType],
                                                      ofItemAtPath: folderPath)
            } catch {
                logI("Realm: Couldn't change Realm file protection type")
            }
        }

Solution

  • Adding the default.realm.lock in a new location did the trick and stops the crashes from appearing again. However I was never able to replicate the crash. This is because it could be happening in background.