swift 3中的核心数据轻量级迁移不起作用
Posted
技术标签:
【中文标题】swift 3中的核心数据轻量级迁移不起作用【英文标题】:core data lightweight migration in swift 3 not working 【发布时间】:2017-08-24 11:21:11 【问题描述】:我正在使用 persistentContainer
lazy var persistentContainer: NSPersistentContainer =
/*
The persistent container for the application. This implementation
creates and returns a container, having loaded the store for the
application to it. This property is optional since there are legitimate
error conditions that could cause the creation of the store to fail.
*/
// let container = NSPersistentContainer(name:"YiuPai")
let container = NSPersistentContainer(name:"YiuPai")
let storeUrl = self.applicationDocumentsDirectory.appendingPathComponent("YiuPai.sqlite")
print(storeUrl)
let description = NSPersistentStoreDescription(url: storeUrl)
description.shouldInferMappingModelAutomatically = true
description.shouldMigrateStoreAutomatically = true
container.persistentStoreDescriptions = [NSPersistentStoreDescription(url: storeUrl)]
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)")
)
return container
()
错误我得到了
2017-08-24 16:37:43.912800+0530 YiuPai[1688:496051] [error] error: -addPersistentStoreWithType:SQLite configuration:(null) URL:file:///var/mobile/Containers/Data/Application/81EC85D8-5C35-4090-8F2E-5229775F2D43/Documents/YiuPai.sqlite options:
NSInferMappingModelAutomaticallyOption = 1;
NSMigratePersistentStoresAutomaticallyOption = 1;
... returned error Error Domain=NSCocoaErrorDomain Code=134110 "An error occurred during persistent store migration." UserInfo=sourceURL=file:///var/mobile/Containers/Data/Application/81EC85D8-5C35-4090-8F2E-5229775F2D43/Documents/YiuPai.sqlite, reason=Can't copy source store to destination store path, destinationURL=file:///var/mobile/Containers/Data/Application/81EC85D8-5C35-4090-8F2E-5229775F2D43/Documents/.YiuPai.sqlite.migrationdestination_41b5a6b5c6e848c462a8480cd24caef3, NSUnderlyingError=0x170851460 Error Domain=NSSQLiteErrorDomain Code=5 "(null)" UserInfo=NSFilePath=/var/mobile/Containers/Data/Application/81EC85D8-5C35-4090-8F2E-5229775F2D43/Documents/.YiuPai.sqlite.migrationdestination_41b5a6b5c6e848c462a8480cd24caef3, Source database Path=/var/mobile/Containers/Data/Application/81EC85D8-5C35-4090-8F2E-5229775F2D43/Documents/YiuPai.sqlite, reason=Failed to replace destination database with userInfo dictionary
NSUnderlyingError = "Error Domain=NSSQLiteErrorDomain Code=5 \"(null)\" UserInfo=NSFilePath=/var/mobile/Containers/Data/Application/81EC85D8-5C35-4090-8F2E-5229775F2D43/Documents/.YiuPai.sqlite.migrationdestination_41b5a6b5c6e848c462a8480cd24caef3, Source database Path=/var/mobile/Containers/Data/Application/81EC85D8-5C35-4090-8F2E-5229775F2D43/Documents/YiuPai.sqlite, reason=Failed to replace destination database";
destinationURL = "file:///var/mobile/Containers/Data/Application/81EC85D8-5C35-4090-8F2E-5229775F2D43/Documents/.YiuPai.sqlite.migrationdestination_41b5a6b5c6e848c462a8480cd24caef3";
reason = "Can't copy source store to destination store path";
sourceURL = "file:///var/mobile/Containers/Data/Application/81EC85D8-5C35-4090-8F2E-5229775F2D43/Documents/YiuPai.sqlite";
我该如何解决这个问题?
【问题讨论】:
【参考方案1】:我想我有你的错误。尝试像这样设置您的 storeUrl:
let storeUrl = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!.appendingPathComponent("YiuPai.sqlite")
【讨论】:
以上是关于swift 3中的核心数据轻量级迁移不起作用的主要内容,如果未能解决你的问题,请参考以下文章
swift - 从 var(UITextField 输入)中删除空格不起作用
从 MVC 迁移到 ASP.NET Core 3.1 中的端点路由时,具有角色的 AuthorizeAttribute 不起作用