简体   繁体   English

领域加密,领域文件解密失败,怎么了?

[英]realm encription, Realm file decryption failed,what‘s wrong?

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. 但是当我迁移数据库时,似乎显示“ Realm文件解密失败”,并且应用程序崩溃了。

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 !!! 帮我 !!!

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]; 另请注意, [configuration setDeleteRealmIfMigrationNeeded:YES]; will delete Realm file in case migration is needed and migration will not be executed. 如果需要迁移并且将不执行迁移,则会删除Realm文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM