将NSManagedObjectContext传递给UITabBarController子视图控制器的最佳实践? [英] Best practices for passing NSManagedObjectContext around to UITabBarController child view controllers?

查看:78
本文介绍了将NSManagedObjectContext传递给UITabBarController子视图控制器的最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以在我看来,这里有一个Catch-22的情况。请注意以下广泛(巧妙)的应用程序架构的立场:

So it seems to me like there's a Catch-22 situation here. Note the following widely (and smartly) held stances on application architecture:


  1. Apple(和大多数alpha开发人员)建议不要使用单例或访问用于检索NSManagedObjectContext的应用程序委托单例。

  2. 在UITabbarController的子视图控制器上迭代,并为每个子VC提供上下文引用(依赖注入)也是很愚蠢的,因为您正在应用程序启动时加载您的应用程序中的每个选项卡,只是为了传递引用。这也违反了Apple对应用程序架构的一切建议。

如何解决这个问题?当视图控制器从nib中唤醒时,是否使用NotificationCenter发布通知,然后让应用程序代理在上下文引用中传递?这是我能用#1和#2看到的那种jives的唯一方法,但它也觉得对我有些扭曲。

How do you resolve this? Do you use NotificationCenter to post a notification when a view controller awakes from a nib, and have the app delegate pass in the context reference then? That's about the only way I can think of that jives with both #1 and #2, but it also feels like some contortion to me.

有更优雅的方式?

编辑:初始化视图控制器时执行通知可能是一种竞争条件,因为如果你使用Storyboard做事情, tabbar的子视图控制器往往在启动时被初始化(虽然无法加载)。所以你必须在viewDidLoad中做一个这样的通知,这是一个坏主意相对于MVC约定。在用户执行任何与视图相关的操作之前,它还会绑定数据模型(例如性能预缓存)。

Edit: doing a notification when initializing a view controller can be a race condition, since if you're doing things with Storyboard, your tabbar's child view controllers tend to be initialized (though sans-view loading) at launch. So you'd have to do such a notification in viewDidLoad, which is a bad idea vis-a-vis MVC convention. It also ties your hands from doing anything with data models (like pre-caching for performance) before the user does anything view-related.

推荐答案

将NSManagedObject实例传递到视图控制器时,该视图控制器可以保留这些对象。然后可以通过调用

When you pass NSManagedObject instances to a view controller, that view controller can retain those objects. It can then get to the NSManagedObjectContext through those managed objects by calling

-[NSManagedObject managedObjectContext]

我不知道这是否会在你的特定情况下工作,但通常它会。应用程序代理或根视图控制器创建上下文,然后传递管理对象。

I'm not sure if this will work in your particular case, but often it will. The app delegate or the root view controller creates a context and then passes along managed objects.

你需要在多个地方使用上下文,还有另一个我发现有用的模式:

If you need to use the context in multiple places, there's another pattern that I've found useful:

子类 NSManagedObjectContext

@interface MyManagedObjectContext : NSManagedObjectContext

+ (MyManagedObjectContext *)mainThreadContext;

@end

UI /主线程的 singleton 上下文。这比使用App委托干净,因为其他类不必得到App委托,但可以直接使用这个类。还可以将商店和模型的初始化封装到此类中:

i.e. a singleton context for the UI / main thread. This is cleaner than using the App delegate, since other classes won't have to get to the App delegate, but can use this class directly. Also the initialization of the store and model can be encapsulated into this class:

@implementation MyManagedObjectContext

+ (MyManagedObjectContext *)mainThreadContext;
{
   static MyManagedObjectContext *moc;
   static dispatch_once_t onceToken;
   dispatch_once(&onceToken, ^{
       moc = [[self alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
       // Setup persistent store coordinator here
   });
   return moc;
}

@end

这篇关于将NSManagedObjectContext传递给UITabBarController子视图控制器的最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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