【问题标题】:Get MagicalRecord NSManagedContext for use in background threads获取 MagicalRecord NSManagedContext 以在后台线程中使用
【发布时间】:2014-10-02 01:29:22
【问题描述】:

我正在使用 MagicalRecord 2.2 并尝试在默认情况下在后台线程上运行我的 fetch 查询,但似乎文档已过时。具体来说:

If you need to create a new managed object context for use in non-main threads, 
use the following method:

NSManagedObjectContext *myNewContext = [NSManagedObjectContext MR_newContext];

但是,缺少MR_newContext 方法(猜测它已被弃用)。有一个[NSManagedObjectContext MR_context] 方法,但我不确定它返回什么上下文。深入研究代码,它会创建一个并发类型为NSPrivateQueueConcurrencyType 的新上下文,所以我猜这就是我要寻找的。​​p>

谁能确认一下?

【问题讨论】:

    标签: ios objective-c iphone magicalrecord magicalrecord-2.2


    【解决方案1】:

    你可能想要使用

    [NSManagedObjectContext MR_confinementContext]
    

    不过,由于 CoreData 团队已经有效地弃用了限制上下文,这个名称也可能会改变。

    【讨论】:

      【解决方案2】:

      我认为你最好使用+ (NSManagedObjectContext *) MR_contextForCurrentThread;。它的实现对于您的目的来说似乎很好:

      + (NSManagedObjectContext *) MR_contextForCurrentThread;
      {
          if ([NSThread isMainThread])
          {
              return [self MR_defaultContext];
          }
          else
          {
              NSMutableDictionary *threadDict = [[NSThread currentThread] threadDictionary];
              NSManagedObjectContext *threadContext = [threadDict objectForKey:kMagicalRecordManagedObjectContextKey];
              if (threadContext == nil)
              {
                  threadContext = [self MR_contextWithParent:[NSManagedObjectContext MR_defaultContext]];
                  [threadDict setObject:threadContext forKey:kMagicalRecordManagedObjectContextKey];
              }
              return threadContext;
          }
      }
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-17
      • 1970-01-01
      • 1970-01-01
      • 2017-11-17
      • 1970-01-01
      • 2018-07-27
      相关资源
      最近更新 更多