【问题标题】:How to add NSManagedObjectContext with ConcerrencyType in swift 3?如何在 swift 3 中添加带有 ConcerrencyType 的 NSManagedObjectContext?
【发布时间】: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()

标签: ios core-data swift3


【解决方案1】:

首先你必须创建一个从privateContextNSPersistentContainerviewContext 的链接。因此,在创建私有的同时, privateContextprivateContext.parentContext = viewContext。保存的同时 let context = privateContext while(context != nil) { context.save() context = context.privateContext } 这将起作用。 我不知道你为什么首先得到错误。

【讨论】:

  • 不一样吗?我创建上下文,即 viewContext,然后将其设置为 privateContext 的 parentContext。关于保存 - 我应该将它添加到 Core Data 堆栈中还是只是在任何地方替换我的保存代码?
  • 创建NSManagedObjectContextextension 以包含save() 方法的传播。所以,不要像saveAndPropagate()那样调用save()
  • context = context.privateContext 你是说context.parent 在这里吗?
  • 好的。 extension NSManagedObject { func privateContext() -> self { let context = NSManagedObjectContext(concurrencyType:.privateQueueConcurrencyType) context.parentContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext return context} func saveAndPropagate() { let context = privateContext while(context != nil) { context.save() context = context.privateContext }} }
  • 我阅读了一些文档并做了几乎相同的工作,以单例形式向 CoreData 发出所有请求,并添加了一个函数来为背景创建上下文。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
相关资源
最近更新 更多