ios NSManagedObjectContext 的 performBlock: 用于什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9922145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is NSManagedObjectContext's performBlock: used for?
提问by nevan king
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.
在 iOS 5 中,NSManagedObjectContext
有几个新方法,performBlock:
和performBlockAndWait:
. 这些方法实际上是用来做什么的?它们在旧版本中替换了什么?什么样的块应该传递给他们?我如何决定使用哪个?如果有人有一些使用它们的例子,那就太好了。
回答by MikeG
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.
如果 MOC 是使用或初始化的,则performBlock:
和方法performBlockAndWait:
用于向您的NSManagedObjectContext
实例发送消息。如果您对这些上下文类型之一执行任何操作,例如设置持久存储或保存更改,则在块中执行。NSPrivateQueueConcurrencyType
NSMainQueueConcurrencyType
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.
performBlock:
将块添加到后备队列并安排它在自己的线程上运行。该块将立即返回。您可以将其用于对后备存储的长期持久操作。
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.
performBlockAndWait:
还将块添加到后备队列并安排它在自己的线程上运行。但是,直到块执行完毕,块才会返回。如果您在知道手术是否成功之前无法继续前进,那么这是您的选择。
For example:
例如:
__block NSError *error = nil;
[context performBlockAndWait:^{
myManagedData.field = @"Hello";
[context save:&error];
}];
if (error) {
// handle the error.
}
Note that because I did a performBlockAndWait:
, I can access the error outside the block. performBlock:
would require a different approach.
请注意,因为我做了一个performBlockAndWait:
,所以我可以在块外访问错误。performBlock:
将需要不同的方法。
From the iOS 5 core data release notes:
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
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.)
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).
Main queue (NSMainQueueConcurrencyType).
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 现在为并发操作提供结构化支持。当您使用 initWithConcurrencyType: 创建托管对象上下文时,您可以为其线程(队列)关联提供三个选项
限制(NSConfinementConcurrencyType)。
这是默认设置。你保证上下文不会被你创建它的线程以外的任何线程使用。(这与您在先前版本中使用的线程要求完全相同。)
私有队列 (NSPrivateQueueConcurrencyType)。
上下文创建和管理一个私有队列。与您创建和管理与上下文关联的线程或队列不同,这里的上下文拥有队列并为您管理所有详细信息(前提是您使用如下所述的基于块的方法)。
主队列(NSMainQueueConcurrencyType)。
上下文与主队列相关联,因此与应用程序的事件循环相关联,但在其他方面类似于基于私有队列的上下文。您可以将此队列类型用于链接到仅需要在主线程上使用的控制器和 UI 对象的上下文。
回答by Sharen Eayrs
They allow you to access the same managedObjectContext
accross threads.
它们允许您访问相同的managedObjectContext
跨线程。
I am not really sure I am correct, but this is how I use it.
我不确定我是否正确,但这就是我使用它的方式。
You use performBlockAndWait
is like "usual". You do not need it if you execute the managedObjectContext only on one thread. If you execute it on many threads then yes you will need performBlock
.
你使用performBlockAndWait
就像“通常”一样。如果仅在一个线程上执行 managedObjectContext,则不需要它。如果您在许多线程上执行它,那么是的,您将需要performBlock
.
So, if you're on main thread, you do not need to do performBlockAndWait
for the main managedObjectContext
. At least I don't and is doing fine.
所以,如果你在主线程上,你不需要performBlockAndWait
为 main做managedObjectContext
。至少我没有,而且做得很好。
However if you access that managedObjectContext
on other threads then yes you will need to do performBlockAndWait
.
但是,如果您managedObjectContext
在其他线程上访问它,那么是的,您将需要执行performBlockAndWait
.
So that's the purpose of performBlock
and performBlockAndWait
.
这就是performBlock
and的目的performBlockAndWait
。
Would someone please correct me if I am wrong here. Of course if you access the context only on one thread then you can simply use the default.
如果我在这里错了,有人请纠正我。当然,如果您仅在一个线程上访问上下文,那么您可以简单地使用默认值。