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

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

问题描述

在iOS 5中, NSManagedObjectContext 有几个新方法, performBlock: performBlockAndWait:。实际使用这些方法是什么?他们在旧版本中替换了什么?什么样的块应该传递给他们?我如何决定使用哪个? c> performBlock:

code>和 performBlockAndWait:用于向您的 NSManagedObjectContext 实例发送消息。 code> NSPrivateQueueConcurrencyType 或 NSMainQueueConcurrencyType 。如果你对这些上下文类型之一做任何事情,例如设置持久存储或保存更改,你可以在块中执行。



performBlock :将把块添加到后备队列并调度它在自己的线程上运行。块将立即返回。您可以将其用于对后备存储库的长持久性操作。



performBlockAndWait:支持队列并调度它在自己的线程上运行。但是,该块将不会返回,直到块完成执行。如果你不知道操作是否成功,那么这是你的选择。



例如:

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

if(error){
//处理错误。注意,因为我做了一个 performBlockAndWait:


,我可以访问块外的错误。 performBlock:需要不同的方法。



iOS 5核心数据发布说明


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




  • Confinement(NSConfinementConcurrencyType )。



    这是默认值。你承诺上下文不会被任何线程使用,而不是你创建它的那个。 (这与以前版本中使用的线程要求完全相同。)


  • 私有队列(NSPrivateQueueConcurrencyType)。



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


  • 主队列(NSMainQueueConcurrencyType)



    上下文与主队列相关联,应用程序的事件循环,但它另外类似于基于专用队列的上下文。



$

b $ b

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.

解决方案

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: 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: 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.

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.

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的performBlock:用于?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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