0

My code is here:

RLMRealmConfiguration *configuration = [RLMRealmConfiguration defaultConfiguration];
NSString *filePath = [NSString stringWithFormat:@"%@",configuration.fileURL];
NSString *dateBaseName = @"aaa";
configuration.fileURL = [NSURL URLWithString:[[[filePath stringByDeletingLastPathComponent]
                                               stringByAppendingPathComponent:dateBaseName]
                                              stringByAppendingPathExtension:@"realm"]];
 configuration.encryptionKey = [self getEncriptionKey];
[RLMRealmConfiguration setDefaultConfiguration:configuration];
NSError *err = nil;
RLMRealm *realm = [RLMRealm realmWithConfiguration:configuration
                                             error:&err];
if (!realm) {
    NSLog(@"Error opening realm: %@", err);
}

but when I migrate the datebase,it appears that "Realm file decryption failed",and the app crashed.

and my migrate code is here:

RLMRealmConfiguration *configuration = [RLMRealmConfiguration defaultConfiguration];
configuration.schemaVersion  = 3;
configuration.encryptionKey = [self getEncriptionKey];
configuration.migrationBlock = migrationBlock;
[configuration setDeleteRealmIfMigrationNeeded:YES];
[RLMRealmConfiguration setDefaultConfiguration:configuration];

help me !!!

SoundShock
  • 485
  • 1
  • 3
  • 24
Line
  • 1

1 Answers1

0

When do you execute your migration code?

Your code should look like this:

RLMRealmConfiguration *configuration = [RLMRealmConfiguration defaultConfiguration];

NSString *filePath = [NSString stringWithFormat:@"%@",configuration.fileURL];
NSString *dateBaseName = @"aaa";

configuration.fileURL = [NSURL URLWithString:[[[filePath stringByDeletingLastPathComponent]
                                               stringByAppendingPathComponent:dateBaseName]
                                              stringByAppendingPathExtension:@"realm"]];
configuration.encryptionKey = [self getEncriptionKey];
configuration.schemaVersion  = 3;
configuration.migrationBlock = migrationBlock;

[RLMRealmConfiguration setDefaultConfiguration:configuration];

NSError *err = nil;
RLMRealm *realm = [RLMRealm realmWithConfiguration:configuration
                                             error:&err];
if (!realm) {
    NSLog(@"Error opening realm: %@", err);
}

Also note that [configuration setDeleteRealmIfMigrationNeeded:YES]; will delete Realm file in case migration is needed and migration will not be executed.

Dmitry
  • 7,300
  • 6
  • 32
  • 55