Ios 在哪里放置persistentStoreDescriptions?在Swift3 Coredata样板堆中
我将Coredata迁移到新版本,并放置persistentStoreDescriptions 在loadPersistentStores和app未崩溃但未持久化数据之前,考虑到数据在迁移之前运行良好,以下是我的代码:Ios 在哪里放置persistentStoreDescriptions?在Swift3 Coredata样板堆中,ios,swift,core-data,swift3,core-data-migration,Ios,Swift,Core Data,Swift3,Core Data Migration,我将Coredata迁移到新版本,并放置persistentStoreDescriptions 在loadPersistentStores和app未崩溃但未持久化数据之前,考虑到数据在迁移之前运行良好,以下是我的代码: lazy var persistentContainer: NSPersistentContainer = { /* The persistent container for the application. This implementation c
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: "CoreTest3")
let description = NSPersistentStoreDescription()
description.shouldInferMappingModelAutomatically = true
description.shouldMigrateStoreAutomatically = true
container.persistentStoreDescriptions = [description]
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
}()
根据文档,在初始化持久存储描述时,必须指定托管对象模型的URL:
let description = NSPersistentStoreDescription(url: storeURL)
要获取URL,请查看Bundle.URL(forResource:withExtension)谢谢在看到您的答案之前我找到了它这正是我提出的问题:让urlStore=directory.appendingPathComponent(“MYAPPName.sqlite”)description.URL=urlStoreBTW谢谢这是答案,太好了,像3天或4天一样努力,我遇到了同样的问题,似乎无法让这部分正常工作。@flu评论中的
目录是什么?