【发布时间】:2012-02-23 18:21:38
【问题描述】:
在一个应用程序中,可能有不同的UIViewControllers 需要共享相同的NSManagedObject。我通常会做以下事情:
@interface CustomController : UIViewController
@property (nonatomic, retain) ProductNSManagedObject* productManaged;
@end
然后,当我创建 CustomController 时,我将其注入如下:
customController.productManaged = ....
一旦完成,CustomController 负责发布它。
这种方法效果很好(我不知道它是否正确),但是当控制器需要该对象但它不是具有该对象的控制器的直接子对象时该怎么办?例如
MainController -> ChildController -> SubChildController -> ....
MainController 拥有托管对象。
我是否必须创建很多中间属性,还是需要执行新的NSFetchRequest 或其他?
同样的方面可以应用于NSManagedObjectContext。环顾四周,我发现可以从拥有它的应用程序委托中获取上下文(如果有的话)。但正如 Marcus Zarra 在 passing-around-a-nsmanagedobjectcontext-on-the-iphone 中所写,这种方法缺乏灵活性。
有什么建议吗?提前谢谢你。
【问题讨论】:
标签: ios core-data properties nsmanagedobject nsmanagedobjectcontext