沿视图/控制器层次结构传递managedObjectContext [英] Passing managedObjectContext along to view/controller hierarchy

查看:107
本文介绍了沿视图/控制器层次结构传递managedObjectContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在大多数情况下,在应用程序委托中创建的受管对象上下文。
将它传递给view / controller层次结构的方法是什么,所以树中的每个导航/标签控制器都可以访问它。每次从父母到孩子传递它可能不是最好的,因为我必须每次都携带这些信息,有时不是所有的控制器到特定的孩子的方式可能需要它。

Managed object context in most cases created in application delegate. What are the ways of passing it along to view/controller hierarchy, so every navigation/tab controller in the tree can have access to it. Passing it every time from parent to child may not be the best since I have to carry this information every time and sometimes not all of the controllers to the way of particular child may need it.

推荐答案

推荐的设计是做一个依赖注入。这意味着当你创建一个 UIViewController 时,你通过一个属性传递 NSManagedObjectContext 。如果 UIViewController 只需要一个或一组 NSManagedObject 实例,那么你只需通过属性来处理这些。

The recommended design is to do a dependency injection. This means that when you create a UIViewController you pass in the NSManagedObjectContext via a property. If the UIViewController only needs a single or set of NSManagedObject instances then you would just hand those in via properties.

此设计更灵活,可以更好地重复使用您的代码。将一切链接到一个单一的硬编码 NSManagedObjectContext 在AppDelegate是脆弱的,降低了您的代码的灵活性。

This design is more flexible and allows for greater reuse of your code. Linking everything to a single "hard-coded" NSManagedObjectContext in the AppDelegate is fragile and reduces the flexibility of your code.

我深入讨论了 Mac开发人员网络

这篇关于沿视图/控制器层次结构传递managedObjectContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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