問題描述
在 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 是使用 NSPrivateQueueConcurrencyType
或 NSMainQueueConcurrencyType
初始化的.如果您對這些上下文類型之一執行任何操作,例如設置持久存儲或保存更改,您可以在一個塊中執行.
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: 是做什么用的?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!