I have an iOS app that uses Realm as a local database. Some users are experiencing issues where they can’t open the Realm file unless they delete and reinstall the app. This issue seems to be random and not happening on every device. I haven’t been able to replicate it on my test device during local development. I’ve been trying to trace this issue for a few days with no luck. What could be causing this error, and how can I trace down the actual cause of this issue?
Can you clarify what process a user would go through to “open a realm file”? Are you exposing the database file in your app? e.g. the user can use some kind of file browser to select and open the file?
If not, how is the file being opened? Do you have the associated code? What is your setup? Podfile? SPM? Versions of software, SDK etc?
No, I’m not exposing the database file in my app. What I mean is when the app tries to create the realm instance, it throws the error “Failed to open Realm file at path ‘…/CustomName.realm’: Operation not permitted. Please use a path where your app has read-write permissions.”
RealmSwift: 10.42.0
Xcode: 15.0
Package Manager: Cocoapods
Encryption: Yes
This is the code that I had configured at the app start (AppDelegate)
The error throws after the realm config is updated and tries to access the realm instance.
public func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
return paths[0]
}
func configureRealm() {
// Set custom db name
let dbUrl = getDocumentsDirectory()
.appendingPathComponent("CustomName.realm")
var config = Realm.Configuration(
// Set db file path
fileURL: dbUrl,
// Set database encryption key
// default is no encryption
encryptionKey: RealmUtil.getKey() as Data,
// Set the new schema version. This must be greater than the previously used
// version (if you've never set a schema version before, the version is 0).
schemaVersion: 10,
// Set the block which will be called automatically when opening a Realm with
// a schema version lower than the one set above
migrationBlock: { _, oldSchemaVersion in
// We haven’t migrated anything yet, so oldSchemaVersion == 0
if oldSchemaVersion < 1 {
print("Migration is done")
}
})
// Tell Realm to use this new configuration object for the default Realm
Realm.Configuration.defaultConfiguration = config
do {
// --> I got error here <--
let realm = try Realm(configuration: config)
} catch {
print("Realm Configure Exception: \(error)")
Current.logger.log(error: "Realm Error: \(error.localizedDescription)")
}
}
Also, do you have a situation where the database is not encrypted but then it’s opened afterwards with the encryption set? If so, that will throw an error.
It is just a static utility function that retrieves an encryption key from keychain access.
That is the same implementation from the realm swift GitHub example.
I don’t think I have a situation like that because the database was set to encrypt at the first app install. If that is the case, the realm should throw an error related to the encryption/decryption problem. Isn’t?