I'm relying on an encrypted realm to store certain data in an app written in Swift. Sometimes I face a situation where I delete a given realm and create a new one to ensure that no data will persist between certain states.
I keep track of encryption keys myself, so there is a risk that I unintentionally attempt to decrypt a realm with a wrong encryption key, which raises the following exception:
libc++abi.dylib: terminating with uncaught exception of type realm::RealmFileException: Unable to open a realm at path '/path/to/private.realm': Realm file decryption failed.
Since this means that I have lost the original encryption key, essentially leaving this particular realm useless, I'd like to be able to delete the realm file and start over instead of crashing.
I create the realm as suggested by the docs:
do {
var configuration = Realm.Configuration.defaultConfiguration
configuration.encryptionKey = ...
try Realm(configuration: configuration)
}
catch let error {
}
I've tried this and similar approaches to catch the NSException
and return it to be handled by Swift code, but there doesn't seem to be an straight forward way to achieve this. Is it impossible, or am I approaching this incorrectly?