目前,我正在对现有 iOS9 应用程序进行 Swift3 / iOS10 更新,该应用程序存储了欧洲各地约 10,000 个电动汽车充电点。到目前为止,我总是为应用程序提供预填充的数据库(.xcappdata 包中的 .sqlite、.sqlite-shm、.sqlite-wal 文件),但在当前版本中,Apple 引入了NS持久化容器 https://developer.apple.com/reference/coredata/nspersistentcontainer类,这使得它变得更加复杂。在我的 AppDelegate 类中,我实例化 NSPercientContainer 对象并将其传递给惰性变量,就像 Apple 在每个示例代码中所做的那样:
lazy var stationDataPersistentContainer: NSPersistentContainer = {
let fileMgr = FileManager.default
let destinationModel = NSPersistentContainer.defaultDirectoryURL()
if !fileMgr.fileExists(atPath: destinationModel.appendingPathComponent("StationData.sqlite").path) {
do {
try fileMgr.copyItem(at: URL(fileURLWithPath: Bundle.main.resourcePath!.appending("/StationData.sqlite")), to: destinationModel.appendingPathComponent("/StationData.sqlite"))
try fileMgr.copyItem(at: URL(fileURLWithPath: Bundle.main.resourcePath!.appending("/StationData.sqlite-shm")), to: destinationModel.appendingPathComponent("/StationData.sqlite-shm"))
try fileMgr.copyItem(at: URL(fileURLWithPath: Bundle.main.resourcePath!.appending("/StationData.sqlite-wal")), to: destinationModel.appendingPathComponent("/StationData.sqlite-wal"))
} catch {
//
}
} else {
//
}
/*
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: "StationData")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
/*
* 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
}()
在 iOS 9 版本中,我将文件复制到适当的目录,如以下代码示例所示:
lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = {
let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("StationData.sqlite")
let fileMgr = NSFileManager.defaultManager()
if !fileMgr.fileExistsAtPath(url.path!) {
do {
try fileMgr.copyItemAtPath(NSBundle.mainBundle().pathForResource("StationData", ofType: "sqlite")!, toPath: self.applicationDocumentsDirectory.URLByAppment("StationData.sqlite").path!)
try fileMgr.copyItemAtPath(NSBundle.mainBundle().pathForResource("StationData", ofType: "sqlite-shm")!, toPath: self.applicationDocumentsDirectory.URLByAppendingPathComponent("StationData.sqlite-shm").path!)
try fileMgr.copyItemAtPath(NSBundle.mainBundle().pathForResource("StationData", ofType: "sqlite-wal")!, toPath: self.applicationDocumentsDirectory.URLByAppendingPathComponent("StationData.sqlite-wal").path!)
} catch {
//
} do {
try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url,
options: [NSMigratePersistentStoresAutomaticallyOption:true, NSInferMappingModelAutomaticallyOption:true])
} catch {
//
}
} else {
//
}
return coordinator
}()
几天来我一直尝试将文件移动到正确的目录,该目录由NSPersistentContainer.defaultDirectoryURL() -> URL
,但每次我收到错误时,该文件已经存在,因为我的 stationDataPersistentContainer 已经初始化,因此 NSPersistentContainer 有足够的时间来生成 sqlite* 文件。即使我尝试复制文件并在覆盖的 init() 函数中初始化 stationDataPersistentContainer,我也无法做到这一点。文档中是否有我遗漏或忽略的内容?这是将应用程序安装时的现有数据复制到 coredata 的最佳/正确/适当的方法。
附录:
仅供参考,我还可以将从 API 获取的 JSON 文件存储到文档目录中并运行 JSON 解析器,但这需要大量资源,尤其是时间!(这个问题也发布在苹果开发者论坛上,等待批准)