gpt4 book ai didi

ios - 如何在 swift 3 中添加带有 ConcerrencyType 的 NSManagedObjectContext?

转载 作者:行者123 更新时间:2023-11-29 00:39:12 26 4
gpt4 key购买 nike

在我的应用程序中,我有下载数据并将它们保存到 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)")
}
}
}

最佳答案

首先,您必须创建从 privateContextNSPercientContainerviewContext 的链接。所以,在创建私有(private)的同时, privateContextprivateContext.parentContext = viewContext。保存时 让上下文 = privateContext
而(上下文!=无){
上下文.save()
上下文 = context.privateContext
这会起作用。我不确定你为什么一开始就收到错误。

关于ios - 如何在 swift 3 中添加带有 ConcerrencyType 的 NSManagedObjectContext?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39911151/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com