gpt4 book ai didi

ios - 在 UIViewControllers 中传递 NSManagedObjects 的方法

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:08:16 26 4
gpt4 key购买 nike

在一个应用程序中,可能有不同的 UIViewController 需要共享相同的 NSManagedObject。我通常会执行以下操作:

@interface CustomController : UIViewController

@property (nonatomic, retain) ProductNSManagedObject* productManaged;

@end

然后,当我实例化 CustomController 时,我会像下面这样注入(inject)它:

customController.productManaged = ....

一旦完成,CustomController 负责释放它。

这种方法效果很好(我不知道它是否正确),但是当 Controller 需要该对象但它不是具有该对象的 Controller 的直接子级时该怎么办?例如

MainController -> ChildController -> SubChildController -> ....

其中 MainController 具有托管对象。

我是否必须创建大量中间属性,或者我是否需要执行一个新的 NSFetchRequest 或其他东西?

相同的方面可以应用于 NSManagedObjectContext。四处搜索我发现上下文可以从拥有它的应用程序委托(delegate)中获取(如果有的话)。但正如 Marcus Zarra 在 passing-around-a-nsmanagedobjectcontext-on-the-iphone 中所写,这种方法缺乏灵 active 。 .

有什么建议吗?提前谢谢你。

最佳答案

我创建了一个单例对象,其中包含将在整个应用程序中使用的托管对象上下文。我将与数据相关的任何支持代码(例如,持久存储协调器)放入此单例中,并将所有 View 和 Controller 信息与其分开。

在一种情况下,我需要另一个线程的托管对象上下文。很明显,重构上下文并将其放入同一个单例中会很有用。然后可以在单例内部完成两个上下文之间的合并。

这帮助我管理了我的代码。您可以考虑一下。

关于ios - 在 UIViewControllers 中传递 NSManagedObjects 的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9418863/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com