Hello, I have an app released last year and about 2% of users are experiencing crashes. Here is the relevant crash data from Crashlytics:
RealmDataManager.swift:65: Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=io.realm Code=20 "Failed to open Realm file at path 'XXXXXXXX/Documents/default.realm': Realm file decryption failed (Decryption failed: 'unable to decrypt after 0 seconds (retry_count=4, from=i != bytes_read, size=16384)')" UserInfo={Error Code=20, NSFilePath=/var/mobile/Containers/Data/Application/CD7F5D3C-7F4D-46A9-9A08-CXXXXXXXXX/Documents/default.realm, Error Name=InvalidDatabase, NSLocalizedDescription=Failed to open Realm file at path '/var/mobile/Containers/Data/Application/CD7F5D3C-7F4D-46A9-9A08-CXXXXXXXXX/Documents/default.realm': Realm file decryption failed (Decryption failed: 'unable to decrypt after 0 seconds (retry_count=4, from=i != bytes_read, size=16384)')}
I encrypted the Realm database following this MongoDB code sample by storing an encryption key in the keychain.
When initializing the Realm, I pass a configuration with the encryption key. This works for most users, but some are seeing crashes indicating a decryption failure when accessing the Realm, especially when they launch the app.
Relevant code:
let configuration: Realm.Configuration = Realm.Configuration(
encryptionKey: getKey(),
// migration settings comes here...
)
final class RealmDataManager {
func getLoginAccountRecord() -> Account? {
// pass configuration in here
let realm = try! Realm(configuration: configuration)
guard let recordId = getLoginAccountRecordId(),
let record = realm.object(ofType: Account.self, forPrimaryKey: recordId) else {
return nil
}
return record
}
}
My question is - is it possible for the decryption to fail randomly even with the correct key? Could there be something wrong with my encryption setup that only fails for some users? Appreciate any pointers on what might be going wrong.