有没有办法从现有的Realm数据库中删除加密?

时间:2016-10-10 13:21:39

标签: encryption realm

我们正在使用Realm的Objective-C版本,版本2.0.2。该数据库目前已加密并位于现场。

Realm启动时出现间歇性崩溃,错误消息“无法在路径上打开领域...... Realm文件解密失败”。我们是Realm的最新版本,但未能找到解决方案。

我们并不需要在设备上加密数据库,因此我们要考虑删除加密。这是一个选项,如果是这样,我们将如何迁移现有的加密数据库?

1 个答案:

答案 0 :(得分:1)

您可以使用writeCopyToURL:encryptionKey:error:nil加密密钥来编写未加密的副本,然后将其移到原始文件上:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    RLMRealmConfiguration *confg = [[RLMRealmConfiguration alloc] init];
    config.encryptionKey = ...;
    NSURL *tempUrl = [NSURL URLWithString:[NSTemporaryDirectory() stringByAppendingPathComponent:"temp.realm"]];

    // Open the Realm within an autoreleasepool so that it's closed before we try
    // to overwrite the original file
    @autoreleasepool {
        RLMRealm *realm = [RLMRealm realmWithConfiguration:config error:nil];
        [realm writeCopyToURL:tempUrl encryptionKey:nil error:nil];
    }

    [[NSFileManager defaultManager] moveItemAtURL:tempUrl toURL:config.fileUrl error:nil];

    // ... other didFinishLaunchingWithOptions things ...

    return YES;
}