I have an application that, after logging out for compliance reasons, should erase all Realm data and change the encryption key.
This is not exactly the same RLMException "Realm at path '' already opened with a different encryption key" after writeCopy (toFile :, encryptionKey :)
I delete all files after all Realm objects have been freed.
+ (void)deleteRealm:(BOOL)emptyDatabase numRetries:(NSUInteger)numRetries { [[NSNotificationCenter defaultCenter] postNotificationName:@"AppDataStoreWillFlushDatabaseNotification" object:self]; if (emptyDatabase) { @autoreleasepool { RLMRealm *realm = [RLMRealm defaultRealm]; [realm transactionWithBlock:^{ [realm deleteAllObjects]; }]; [realm invalidate]; } } NSFileManager *manager = [NSFileManager defaultManager]; RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration]; NSArray<NSURL *> *realmFileURLs = @[ config.fileURL, [config.fileURL URLByAppendingPathExtension:@"lock"], [config.fileURL URLByAppendingPathExtension:@"log_a"], [config.fileURL URLByAppendingPathExtension:@"log_b"], [config.fileURL URLByAppendingPathExtension:@"note"], [[config.fileURL URLByDeletingLastPathComponent] URLByAppendingPathComponent:[NSString stringWithFormat:@"%@.realm.management", [[config.fileURL URLByDeletingPathExtension] lastPathComponent]]] ]; for (NSURL *URL in realmFileURLs) { NSError *error = nil; [manager removeItemAtURL:URL error:&error]; if (error) {
And recreation with:
+ (void)configureRealm:(NSUInteger)numRetries { // Setup the encryption key NSString *encryptionKey = [CHPasswordManager encryptedStorePassphrase]; // If encryption key is not valid anymore, generate a new one (CoreData used a 32 chars string while Realm uses a 64 chars string) if (encryptionKey.length == 32) { [CHPasswordManager wipeKeyFromSecurityEnclave]; encryptionKey = [CHPasswordManager encryptedStorePassphrase]; } NSData *key = [encryptionKey dataUsingEncoding:NSUTF8StringEncoding]; RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration]; config.encryptionKey = key; // Set this as the configuration used for the default Realm [RLMRealmConfiguration setDefaultConfiguration:config]; @autoreleasepool { @try { [RealmUser allObjects]; }@catch (NSException *e) { if ([e.name isEqual:@"RLMException"]) { // Something went wrong with encryption key - delete database and recreate again if (numRetries >= kRetriesThreshold) { [Bugsnag notify:e]; } else { [self deleteRealm:NO numRetries:numRetries]; } } } } }
However, I often get an RLMException:
"reason: 'Kingdom on the way' / Users / myname / Library / Developer / CoreSimulator / Devices / 152AC3D5-FD24-40DD-AFD7-5A3C4F6EE282 / data / Containers / Data / Application / 2F0140CF-68E4-4D0E-8AC0-BB869BEE9BF8 / Documents /default.realm 'is already open with a different encryption key "
Itβs funny that:
'/Users/MyName/Library/Developer/CoreSimulator/Devices/152AC3D5-FD24-40DD-AFD7-5A3C4F6EE282/data/Containers/Data/Application/2F0140CF-68E4-4D0E-8AC0-BB869.Deme
Not even in the file system.
Any hints to fix the problem?
ios objective-c realm
ppaulojr
source share