Core data 如何更新Swift 4应用程序的EncryptedCoreData实现(NSSQLiteErrorDomain错误14)
我正在重构一个订单处理应用程序,使其包含CoreData,这样它就可以缓存大量数据,而其中一些数据包含一些我宁愿加密也不愿加密的身份敏感信息。该应用程序将在黑客的大目标位置使用。所以我决定在我走得太远之前,我最好用一些东西来保护数据库 事实证明,遵循文章中的说明比作者暗示的要困难得多,至少对于像我这样的CoreData初学者来说是如此 我确实安装了pod并设置了一个桥接头,这样我就可以在我的Swift项目中访问它,但不知道他引用的代码应该放在哪里,或者使用什么URL,等等 类似问题的公认答案对我来说很有意义,我复制了它,但它没有更新为Swift 4,我得到了两个错误:Core data 如何更新Swift 4应用程序的EncryptedCoreData实现(NSSQLiteErrorDomain错误14),core-data,swift4,encrypted-core-data-sql,Core Data,Swift4,Encrypted Core Data Sql,我正在重构一个订单处理应用程序,使其包含CoreData,这样它就可以缓存大量数据,而其中一些数据包含一些我宁愿加密也不愿加密的身份敏感信息。该应用程序将在黑客的大目标位置使用。所以我决定在我走得太远之前,我最好用一些东西来保护数据库 事实证明,遵循文章中的说明比作者暗示的要困难得多,至少对于像我这样的CoreData初学者来说是如此 我确实安装了pod并设置了一个桥接头,这样我就可以在我的Swift项目中访问它,但不知道他引用的代码应该放在哪里,或者使用什么URL,等等 类似问题的公认答案对我
lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "DataModel")
let containerOptions : NSDictionary = [
EncryptedStorePassphraseKey : "someKey",
EncryptedStore.optionFileManager() : EncryptedStoreFileManager.default()// Error: Use of unresolved identifier 'EncryptedStoreFileManager'
]
let desc = try! EncryptedStore.makeDescription(options: containerOptions as! [AnyHashable : Any], configuration: nil)// Error: Type 'EncryptedStore' has no member 'makeDescription'; did you mean 'debugDescription'?
container.persistentStoreDescriptions = [desc]
container.loadPersistentStores(completionHandler:
{ (storeDescription, error) in
if let error = error as NSError?
{
// Replace this implementation with code to handle the error appropriately.
/*
Typical reasons for an error here include:
* The parent directory does not exist, cannot be created, or disallows writing.
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
* The device is out of space.
* The store could not be migrated to the current model version.
Check the error message to determine what the actual problem was.
*/
fatalError("Unresolved error \(error), \(error.userInfo)")
}
}
)
return container
}()
通过注释掉代码的这一部分,我可以很容易地解决“错误:使用未解析的标识符‘EncryptedStoreFileManager’”——我怀疑它不再必要了。第二个错误更难——EncryptedStoreDescription不是我可以看到的类,因此我无法访问初始值设定项,EncryptedStore不再具有创建描述的方法
我错过了什么
编辑:以下尝试的解决方案无效
我想出了以下未经测试的实现,我希望我会取得一些成功。如果我遇到麻烦,将更新
lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "DataModel")
container.loadPersistentStores(completionHandler:
{ (storeDescription, error) in
if let error = error as NSError?
{
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
/*
Typical reasons for an error here include:
* The parent directory does not exist, cannot be created, or disallows writing.
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
* The device is out of space.
* The store could not be migrated to the current model version.
Check the error message to determine what the actual problem was.
*/
fatalError("Unresolved error \(error), \(error.userInfo)")
}
}
)
do
{
let containerOptions : NSDictionary = [
EncryptedStorePassphraseKey : "someKey"
]
guard let appSupportDirURL = FileManager.default.urls(for:.applicationSupportDirectory, in:.userDomainMask).last else
{
throw RuntimeError("appSupportDirURL was nil")
}
try FileManager.default.createDirectory(at: appSupportDirURL, withIntermediateDirectories: true, attributes: nil)
try container.persistentStoreCoordinator.addPersistentStore(ofType: EncryptedStoreType, configurationName: nil, at: appSupportDirURL, options: containerOptions as? [AnyHashable : Any])
}
catch
{
print("WARNING!! : "+error.localizedDescription)
}
return container
}()
编辑:
虽然它可以编译,但我在这个实现中遇到了以下错误:
2019-03-06 17:01:36.771430-0700 Yah-El[1060:1310006] [error] error: -addPersistentStoreWithType:EncryptedStore configuration:(null) URL:file:///var/mobile/Containers/Data/Application/AF6374B3-9127-4E2E-9CAF-D9C89D050D51/Documents/ options:{
EncryptedStorePassphrase = someKey;
} ... returned error Error Domain=NSSQLiteErrorDomain Code=14 "(null)" UserInfo={EncryptedStoreErrorMessage=unable to open database file} with userInfo dictionary {
EncryptedStoreErrorMessage = "unable to open database file";
}
CoreData: error: -addPersistentStoreWithType:EncryptedStore configuration:(null) URL:file:///var/mobile/Containers/Data/Application/AF6374B3-9127-4E2E-9CAF-D9C89D050D51/Documents/ options:{
EncryptedStorePassphrase = someKey;
} ... returned error Error Domain=NSSQLiteErrorDomain Code=14 "(null)" UserInfo={EncryptedStoreErrorMessage=unable to open database file} with userInfo dictionary {
EncryptedStoreErrorMessage = "unable to open database file";
}
WARNING!! : The operation couldn’t be completed. (NSSQLiteErrorDomain error 14.)
使用第二种方法时,您非常接近,但不是将
appSupportDirURL
(文件夹!)传递到addPersistentStore()
,而是传递一个数据库文件:
let storeURL = appSupportDirURL.appendingPathComponent("YourDatabaseName.sqlite")
let store = try container.persistentStoreCoordinator.addPersistentStore(ofType: EncryptedStoreType, configurationName: nil, at: storeURL, options: containerOptions as? [AnyHashable : Any])
但我认为,与其处理路径,不如设置persistentstorescriptions
:
let container = NSPersistentContainer(name: "YourContainerName")
do {
let options = [
EncryptedStorePassphraseKey : "YourPassword"
]
let description = try EncryptedStore.makeDescription(options: options, configuration: nil)
container.persistentStoreDescriptions = [ description ]
}
catch {
NSLog("Could not initialize encrypted database storage: " + error.localizedDescription)
}
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
哦,伙计,谢谢你的回答,但我决定重构整个应用程序,扔掉CoreData,只需使用加密的SQLite包并构建我自己的数据库管理器。事实证明,找出我自己的实现比试图找出CoreData更容易。