【发布时间】:2016-10-07 06:53:56
【问题描述】:
在我的应用程序中,我有下载数据并将其保存到 CoreData 的任务。在我迁移到 swift 3 之后,它开始在随机时间保存数据时抛出异常。据我了解,如果我对所有操作使用一个上下文,就会发生这种情况。现在我创建了另一个并发类型的上下文,一切正常,但没有任何内容保存到 .sqlite 文件:) 以下是我创建上下文的方式:
static let context : NSManagedObjectContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
static let privateContext = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.privateQueueConcurrencyType)
static func declare()
{
AixmParser.privateContext.parent = AixmParser.context
}
我这样保存数据:
do{
try privateContext.save()
}
catch{
}
我是否必须在声明或核心数据堆栈中添加一些内容?
更新:核心数据堆栈如下。
// MARK: - Core Data stack
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: "AppName")
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)")
}
container.viewContext.perform({
// actions upon the NSMainQueueConcurrencyType NSManagedObjectContext for this container
})
})
return container
}()
// MARK: - Core Data Saving support
func saveContext () {
let context = persistentContainer.viewContext
if context.hasChanges {
do {
try context.save()
} catch {
// 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.
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
【问题讨论】:
-
需要看看您是如何设置核心数据堆栈的。
save()保存值并将其推送到parentContext。您必须将while循环中的save()链接到没有父级的最后一个上下文。此上下文直接连接到persistentStoreCoordinator。 -
@New16,我添加了我的核心数据堆栈。我应该如何链接
save()?