作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的应用程序中,我有下载数据并将它们保存到 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)")
}
}
}
最佳答案
首先,您必须创建从 privateContext
到 NSPercientContainer
的 viewContext
的链接。所以,在创建私有(private)的同时, privateContext
,privateContext.parentContext = viewContext
。保存时 让上下文 = privateContext
这会起作用。我不确定你为什么一开始就收到错误。
而(上下文!=无){
上下文.save()
上下文 = context.privateContext
关于ios - 如何在 swift 3 中添加带有 ConcerrencyType 的 NSManagedObjectContext?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39911151/
在我的应用程序中,我有下载数据并将它们保存到 CoreData 的任务。在我迁移到 swift 3 之后,它开始在随机时间保存数据时抛出异常。据我了解,如果我对所有操作使用一个上下文,就会发生这种情况
我是一名优秀的程序员,十分优秀!