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];
[RLMRealmConfiguration setDefaultConfiguration:configuration];
NSError *err = nil;
RLMRealm *realm = [RLMRealm realmWithConfiguration:configuration
                                             error:&err];
if (!realm) {
    NSLog(@"Error opening realm: %@", err);
}

但是当我迁移数据库时,似乎“领域文件解密失败”,应用程序崩溃了。

我的迁移代码在这里:

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

帮我 !!!

4

1 回答 1

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 文件。

于 2016-06-14T13:16:27.743 回答