NSManagedObjectContext的performBlockAndWait:未在接收者的队列上执行 [英] NSManagedObjectContext's performBlockAndWait: not executing on the receiver's queue

查看:75
本文介绍了NSManagedObjectContext的performBlockAndWait:未在接收者的队列上执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,带有 NSMainQueueConcurrencyType NSManagedObjectContext performBlockAndWait的可能:并在接收方(主)队列以外的队列上执行该块。

I've noticed that it's possible for an NSManagedObjectContext with an NSMainQueueConcurrencyType to performBlockAndWait: and execute the block on a queue other than the receiver's (main) queue.

例如,以下代码导致我的 parentContext 如果我的 parentContext childContext 队列上执行块类型为 NSMainQueueConcurrencyType ,而我的 childContext 类型为 NSPrivateQueueConcurrencyType

For example, the following code results in my parentContext executing the block on the childContext's queue if my parentContext is of type NSMainQueueConcurrencyType and my childContext is of type NSPrivateQueueConcurrencyType:

[childContext performBlockAndWait:^{
    //Thread 1, Queue: NSManagedObjectContext Queue
    [parentContext performBlockAndWait:^{
        //Thread 1, Queue: NSManagedObjectContext Queue
        //This is the same queue as the child context's queue
    }];
}];

相反,以下代码按预期方式工作–我的 parentContext 在主队列上执行块:

In contrast, the following code works as expected – my parentContext executes the block on the main queue:

[childContext performBlock:^{
    [parentContext performBlockAndWait:^{
        //Thread 1, Queue: com.apple.main-thread
    }];
}];

这是预期的行为吗?因为文档状态 performBlockAndWait:在接收者的队列上同步执行给定的块,所以这肯定使我感到困惑。

Is this the expected behavior? It is certainly confusing me since the docs state "performBlockAndWait: synchronously performs a given block on the receiver’s queue."

推荐答案

您不必担心执行了哪些线程块。 performBlock: performBlockAndWait:方法保证的是线程安全。因此,从主线程调用 performBlockAndWait:并不意味着将上下文切换到后台线程-这非常昂贵,不需要。如果在块操作期间(在主线程上)尝试执行一个块,则它将被阻塞,直到当前执行的块完成为止。最终,结果将与执行上下文切换相同,只是速度更快。另一方面,调用 performBlock:会将块在任意队列中排队,通常在后台线程上执行。

You should not be worried on what thread blocks are executed. What the performBlock: and performBlockAndWait: methods guarantee is thread safety. Thus, calling performBlockAndWait: from the main thread does not mean there would be a context switch to a background thread - it is very expensive and it is not needed. If during the operation of the block (on the main thread), an attempt is performed to perform a block, it would be blocked until the currently executing block is finished. At the end of the day, the result would be the same as if a context switch was performed, only faster. On the other hand, calling performBlock: will queue the block on an arbitrary queue, often executing on a background thread.

在上面的示例中,由于您 performBlockAndWait:,您的私有队列上下文和主上下文块一样在主线程上执行您的块。在第二个示例中,您将块安排为异步运行,因此它是在后台线程上执行的。

In the example above, since you performBlockAndWait:, your private queue context executes your block on the main thread, as does the main context block. In your second example, you schedule the block to run asynchronously, so it is executed on a background thread.

您不应以其名称来判断线程队列。要查看您是否在主队列中,可以使用 dispatch_get_current_queue()并测试其是否等于 dispatch_get_main_queue()

You should not judge a thread's queue by it's name. To see if you are on the main queue, you can use dispatch_get_current_queue() and test if it is equal to dispatch_get_main_queue().

这篇关于NSManagedObjectContext的performBlockAndWait:未在接收者的队列上执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆