核心数据 Perform() PerformAndWait() 不确定 100%

P S*_*P S 2 core-data ios swift

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

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

1) .mainQueueConcurrencyType (main Queue)
2) .privateQueueConcurrencyType (background Queue)
Run Code Online (Sandbox Code Playgroud)

对于这两种情况,我都可以理解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.
Run Code Online (Sandbox Code Playgroud)

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

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

Tom*_*ton 7

当我们在 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 ...
}
Run Code Online (Sandbox Code Playgroud)