【问题标题】:Core Data Perform() performAndWait() not sure 100% about核心数据 Perform() performAndWait() 不确定 100% 关于
【发布时间】:2018-08-12 05:41:28
【问题描述】:

我有点不确定我是否理解 Core Data 中 perform() 和 performAndWait() 函数的概念。

我们可以有两种类型的上下文:

1) .mainQueueConcurrencyType (main Queue)
2) .privateQueueConcurrencyType (background Queue)

对于这两种情况,我都能理解 performAndWait() 的目的 它等待执行块代码,然后继续。

对于 privateQueueConcurrencyType,文档说:

The NSPrivateQueueConcurrencyType configuration creates its own queue upon initialization and can be used only on that queue. Because the queue is private and internal to the NSManagedObjectContext instance, it can only be accessed through the performBlock: and the performBlockAndWait: methods.

但是 mainQueue 中 perform() 的目的是什么?

当我们在 mainQueue 上有一个上下文并且我们更新/删除/任何一个 NSManagedObject 时,mainQueue 上不会发生这种情况吗?那么 perform() 的目的是什么?

【问题讨论】:

  • 一个优点是您不必关心您当前所在的队列。 CoreData 为您处理。
  • 但是当我在上下文(主)中更新 NSManagedObject 时,我必须使用 perform() 吗?有什么区别吗?
  • 没有。您不必这样做。

标签: ios swift core-data


【解决方案1】:

当我们在 mainQueue 上有一个上下文并且我们更新/删除/任何一个 NSManagedObject 时,mainQueue 上不会发生这种情况吗?

如果您从其他队列调用这些方法怎么办?如果代码在其他队列上执行,它可以在使用主队列并发的托管对象上下文上调用performperformBlockAndWait。即使调用代码不在主队列中,该闭包中的代码也会在主队列中执行。

例如:

let customQueue = DispatchQueue(label: "queuename")
customQueue.async { 
    // ... do some stuff ..
    mainQueueContext.performAndWait {
        // ... do some stuff on the main queue ...
    }
    // ... do some more stuff ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 2010-10-29
    • 2012-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多