NSManagedObjectContext 的 performBlock: 是做什么用的? [英] What is NSManagedObjectContext's performBlock: used for?

查看:22
本文介绍了NSManagedObjectContext 的 performBlock: 是做什么用的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 iOS 5 中,NSManagedObjectContext 有几个新方法,performBlock:performBlockAndWait:.这些方法实际上是用来做什么的?它们在旧版本中替换了什么?应该将什么样的块传递给他们?我如何决定使用哪个?如果有人有一些使用示例,那就太好了.

In iOS 5, NSManagedObjectContext has a couple of new methods, performBlock: and performBlockAndWait:. What are these methods actually used for? What do they replace in older versions? What kind of blocks are supposed to be passed to them? How do I decide which to use? If anyone has some examples of their use it would be great.

推荐答案

方法 performBlock:performBlockAndWait: 用于向您的 NSManagedObjectContext 实例,如果 MOC 是使用 NSPrivateQueueConcurrencyTypeNSMainQueueConcurrencyType 初始化的.如果您对这些上下文类型之一执行任何操作,例如设置持久存储或保存更改,您可以在一个块中执行.

The methods performBlock: and performBlockAndWait: are used to send messages to your NSManagedObjectContext instance if the MOC was initialized using NSPrivateQueueConcurrencyType or NSMainQueueConcurrencyType. If you do anything with one of these context types, such as setting the persistent store or saving changes, you do it in a block.

performBlock: 将块添加到后备队列并安排它在自己的线程上运行.该块将立即返回.您可以将其用于对后备存储的长期持久操作.

performBlock: will add the block to the backing queue and schedule it to run on its own thread. The block will return immediately. You might use this for long persist operations to the backing store.

performBlockAndWait: 还将块添加到后备队列并安排它在自己的线程上运行.但是,在块执行完成之前,块不会返回.如果您在知道操作是否成功之前无法继续前进,那么这是您的选择.

performBlockAndWait: will also add the block to the backing queue and schedule it to run on its own thread. However, the block will not return until the block is finished executing. If you can't move on until you know whether the operation was successful, then this is your choice.

例如:

__block NSError *error = nil;
[context performBlockAndWait:^{
    myManagedData.field = @"Hello";
    [context save:&error];
}];

if (error) {
    // handle the error.
}

请注意,因为我做了一个 performBlockAndWait:,我可以访问块外的错误.performBlock: 需要不同的方法.

Note that because I did a performBlockAndWait:, I can access the error outside the block. performBlock: would require a different approach.

来自 iOS 5 核心数据发行说明:

NSManagedObjectContext 现在为并发操作提供结构化支持.当您使用 initWithConcurrencyType: 创建托管对象上下文时,您有三个选项用于其线程(队列)关联

NSManagedObjectContext now provides structured support for concurrent operations. When you create a managed object context using initWithConcurrencyType:, you have three options for its thread (queue) association

  • 限制(NSConfinementConcurrencyType).

  • Confinement (NSConfinementConcurrencyType).

这是默认设置.您保证上下文不会被除您创建它的线程之外的任何线程使用.(这与您在以前版本中使用的线程要求完全相同.)

This is the default. You promise that context will not be used by any thread other than the one on which you created it. (This is exactly the same threading requirement that you've used in previous releases.)

私有队列(NSPrivateQueueConcurrencyType).

Private queue (NSPrivateQueueConcurrencyType).

上下文创建并管理一个私有队列.在这里,上下文拥有队列并为您管理所有细节,而不是您创建和管理与上下文相关联的线程或队列(前提是您使用如下所述的基于块的方法).

The context creates and manages a private queue. Instead of you creating and managing a thread or queue with which a context is associated, here the context owns the queue and manages all the details for you (provided that you use the block-based methods as described below).

主队列(NSMainQueueConcurrencyType).

Main queue (NSMainQueueConcurrencyType).

上下文与主队列相关联,因此与应用程序的事件循环相关联,但在其他方面类似于私有的基于队列的上下文.您将此队列类型用于链接到控制器和 UI 对象的上下文,这些对象只需要在主线程上使用.

The context is associated with the main queue, and as such is tied into the application’s event loop, but it is otherwise similar to a private queue-based context. You use this queue type for contexts linked to controllers and UI objects that are required to be used only on the main thread.

这篇关于NSManagedObjectContext 的 performBlock: 是做什么用的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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