我的代码在这里:
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);
}
但是当我迁移数据库时,似乎" Realm文件解密失败"并且应用程序崩溃了。
我的迁移代码在这里:
RLMRealmConfiguration *configuration = [RLMRealmConfiguration defaultConfiguration];
configuration.schemaVersion = 3;
configuration.encryptionKey = [self getEncriptionKey];
configuration.migrationBlock = migrationBlock;
[configuration setDeleteRealmIfMigrationNeeded:YES];
[RLMRealmConfiguration setDefaultConfiguration:configuration];
帮助我!!!
答案 0 :(得分:0)
您何时执行迁移代码?
您的代码应如下所示:
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);
}
另请注意,如果需要迁移,[configuration setDeleteRealmIfMigrationNeeded:YES];
将删除Realm文件,并且不会执行迁移。