<small id='7zCD9'></small><noframes id='7zCD9'>

      <tfoot id='7zCD9'></tfoot>
        • <bdo id='7zCD9'></bdo><ul id='7zCD9'></ul>

      1. <i id='7zCD9'><tr id='7zCD9'><dt id='7zCD9'><q id='7zCD9'><span id='7zCD9'><b id='7zCD9'><form id='7zCD9'><ins id='7zCD9'></ins><ul id='7zCD9'></ul><sub id='7zCD9'></sub></form><legend id='7zCD9'></legend><bdo id='7zCD9'><pre id='7zCD9'><center id='7zCD9'></center></pre></bdo></b><th id='7zCD9'></th></span></q></dt></tr></i><div id='7zCD9'><tfoot id='7zCD9'></tfoot><dl id='7zCD9'><fieldset id='7zCD9'></fieldset></dl></div>
        <legend id='7zCD9'><style id='7zCD9'><dir id='7zCD9'><q id='7zCD9'></q></dir></style></legend>

        NSManagedObjectContext 的 performBlock: 是做什么用的?

        时间:2023-06-01
          <tbody id='gtCpw'></tbody>
        <legend id='gtCpw'><style id='gtCpw'><dir id='gtCpw'><q id='gtCpw'></q></dir></style></legend>

        <small id='gtCpw'></small><noframes id='gtCpw'>

        • <bdo id='gtCpw'></bdo><ul id='gtCpw'></ul>

              <tfoot id='gtCpw'></tfoot>
              1. <i id='gtCpw'><tr id='gtCpw'><dt id='gtCpw'><q id='gtCpw'><span id='gtCpw'><b id='gtCpw'><form id='gtCpw'><ins id='gtCpw'></ins><ul id='gtCpw'></ul><sub id='gtCpw'></sub></form><legend id='gtCpw'></legend><bdo id='gtCpw'><pre id='gtCpw'><center id='gtCpw'></center></pre></bdo></b><th id='gtCpw'></th></span></q></dt></tr></i><div id='gtCpw'><tfoot id='gtCpw'></tfoot><dl id='gtCpw'><fieldset id='gtCpw'></fieldset></dl></div>

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

                  问题描述

                  限时送ChatGPT账号..

                  在 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: 是做什么用的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:语义问题:属性的合成 getter 遵循 Cocoa 命名约定以 下一篇:drawRect 圆和动画大小/颜色

                  相关文章

                  最新文章

                1. <i id='PtYEh'><tr id='PtYEh'><dt id='PtYEh'><q id='PtYEh'><span id='PtYEh'><b id='PtYEh'><form id='PtYEh'><ins id='PtYEh'></ins><ul id='PtYEh'></ul><sub id='PtYEh'></sub></form><legend id='PtYEh'></legend><bdo id='PtYEh'><pre id='PtYEh'><center id='PtYEh'></center></pre></bdo></b><th id='PtYEh'></th></span></q></dt></tr></i><div id='PtYEh'><tfoot id='PtYEh'></tfoot><dl id='PtYEh'><fieldset id='PtYEh'></fieldset></dl></div>
                  <legend id='PtYEh'><style id='PtYEh'><dir id='PtYEh'><q id='PtYEh'></q></dir></style></legend>
                  • <bdo id='PtYEh'></bdo><ul id='PtYEh'></ul>
                    1. <tfoot id='PtYEh'></tfoot>

                      <small id='PtYEh'></small><noframes id='PtYEh'>