我正在使用Swift的Realm Objective-C,因为我在我的应用程序中支持iOS7。
我使用一个函数来编写一段代码到后台线程中的realm。我想要添加加密,所以我修改了函数如下:
class func updateRealmWithBlockInBackground(block: () -> Void) {
let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0)) {
do {
let config = RLMRealmConfiguration.defaultConfiguration()
config.encryptionKey = Utils.getKey()
let realm = try RLMRealm(configuration: config)
realm.beginWriteTransaction()
block()
realm.commitWriteTransaction()
} catch {
dispatch_async(dispatch_get_main_queue(), {
TXNotificationSystem.postGlobalNotification(text: "\(error)", textColor: UIColor.redColor())
})
}
}
}
我收到错误代码2:无法解密领域。
如果我使用在主线程上执行写操作的线程,我似乎不会得到这个错误。
有人知道为什么它会给我这个错误吗?
发布于 2015-11-02 08:18:16
如果您的领域以前未加密,则不能仅通过配置加密密钥来添加加密。Realm会假设磁盘上的文件已经加密,并尝试解密它。
您需要在没有加密密钥的情况下打开领域,并使用- [RLMRealm writeCopyToPath:encryptionKey:error:]
编写加密副本。然后,您可以删除未加密的原始.realm文件,并使用加密密钥集打开加密的副本。
https://stackoverflow.com/questions/33445694
复制相似问题