Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Core Data Perform()performAndWait()无法100%确定_Ios_Swift_Core Data - Fatal编程技术网

Ios Core Data Perform()performAndWait()无法100%确定

Ios Core Data Perform()performAndWait()无法100%确定,ios,swift,core-data,Ios,Swift,Core Data,我有点不确定我是否理解核心数据中的perform()和performAndWait()函数的概念 我们可以有两种类型的上下文: 1) .mainQueueConcurrencyType (main Queue) 2) .privateQueueConcurrencyType (background Queue) 对于这两种情况,我都能理解performAndWait()的目的 它等待执行块代码,然后继续 对于privateQueueConcurrencyType,文档显示: The NSPri

我有点不确定我是否理解核心数据中的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中执行()的目的是什么

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

当我们在mainQueue上有一个上下文,并且我们更新/删除了NSManagedObject时,它在mainQueue上不会发生吗

如果您从其他队列调用这些方法呢?如果代码正在其他队列上执行,它可以对使用主队列并发性的托管对象上下文调用
perform
performBlockAndWait
。该闭包中的代码在主队列上执行,即使调用代码不在主队列上

例如:

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

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