使用Core Date + Magical Record传递ManagedContext [英] Passing a ManagedContext with Core Date + Magical Record

查看:88
本文介绍了使用Core Date + Magical Record传递ManagedContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我通过在ViewDidLoad中执行以下操作来设置我的NSManagedContext:

Currently I'm setting my NSManagedContext's by doing the following in ViewDidLoad:

.h

@property (nonatomic,strong) NSManagedObjectContext* managedObjectContext;

.m

 viewDidLoad {
 _managedObjectContext = [NSManagedObjectContext MR_contextForCurrentThread];
 }

问题:我必须通过管理上下文到我想要访问这个ManagedContexts的所有后续视图控制器,或者我可以为每个新的视图控制器的.h写一个新的 @property ,然后设置每个新的viewcontrollers .M中的ManagedObject与 _managedObjectContext = [NSManagedObjectContext MR_contextForCurrentThread];

Question: Do I have to pass this same managed context to all of my subsequent viewcontrollers that I want to have access to this ManagedContexts, or can I write a new @property for each new viewcontroller's .h and then set each new viewcontrollers ManagedObject in .m with _managedObjectContext = [NSManagedObjectContext MR_contextForCurrentThread];

推荐答案

当Core Data堆栈建立时,MagicalRecord会创建一个主队列并发类型的默认上下文。如果所有的视图控制器使用这个默认上下文,你可以

When the Core Data stack is set up, MagicalRecord creates a default context of the "main queue concurrency type". If all your view controllers use this default context, you can


  • (1)将上下文从一个视图控制器传递到下一个, / li>
  • (2)在每个视图控制器中调用 [NSManagedObjectContext MR_defaultContext] 以获取
    默认上下文,

  • (1) pass the context around from one view controller to the next,
  • (2) call [NSManagedObjectContext MR_defaultContext] in each view controller to get the default context,

并且您现在可以。

and you could also, as you currently do


  • (3)在 viewDidLoad 中调用 [NSManagedObjectContext MR_contextForCurrentThread] li>
  • (3) call [NSManagedObjectContext MR_contextForCurrentThread] in viewDidLoad to get the default context.

但是最后一个方法只是因为 viewDidLoad 总是在主线程上调用,
MR_contextForCurrentThread 返回此情况下的默认上下文。

But the last method works only because viewDidLoad is always called on the main thread and MR_contextForCurrentThread returns the default context in that case.

但是, MR_contextForCurrentThread 如果从非主
线程调用,则创建额外的上下文(私有队列并发类型),并将上下文与固定的 NSThread 。但是,正如@casademora正确地说,这样的私有队列上下文不总是使用相同的线程为每个
操作。因此 MR_contextForCurrentThread 不应在非主线程上使用
,它与 MR_defaultContext 相同if从主线程调用。

However, MR_contextForCurrentThread creates additional contexts (of the private queue concurrency type) if called from a non-main thread, and associates the context with a fixed NSThread. But, as @casademora correctly said, such a private queue context does not always use the same thread for each operation. So MR_contextForCurrentThread should not be used on a non-main thread, and it is identical to MR_defaultContext if called from the main thread.

因此,即使它在你的情况下,你应该避免方法(3)。是否选择
方法(1)或(2)纯粹是一个品味问题。

Therefore, even if it works in your case, you should avoid method (3). Whether you choose method (1) or (2) is purely a matter of taste.

如果您需要其他上下文,例如对于后台导入操作,您可以调用
例如。 MR_context MR_contextWithStoreCoordinator 并将该上下文传递给需要

If you need an additional context, e.g. for background import operations, you can call e.g. MR_context or MR_contextWithStoreCoordinator and pass that context to whereever it is needed.

这篇关于使用Core Date + Magical Record传递ManagedContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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