objective-c - 在 Document 的 ArrayController 中访问 appdelegate 的 managedObjectContext

标签 objective-c cocoa core-data swift nsmanagedobjectcontext

我刚刚学习了一些核心数据,并且遇到了一个问题,即拥有并能够访问应用程序范围和仅文档数据的单独存储。

我有一个基于文档的应用程序,该应用程序当前使用 managedObjectContext 中的一些实体通过 NSArrayController 填充表。

但是,其中一些实体应该是应用程序范围的(所有文档体验的一部分 - 例如代表常用工具的按钮数据)

因此,出于这个原因,我粘贴了基于非文档的应用程序的 AppDelegate 代码,它为应用程序创建了一个单独的应用程序范围的存储和 managedObjectContext。我想要做的是通过数组 Controller (和以前一样)在每个 NSPersistentDocument(只读)中访问此 MOC,以填充每个打开的文档的 TableView 。

如何最好地做到这一点?如果这不是从应用程序范围的 MOC 填充出现在每个文档中的 TableView 的方法,我应该寻找哪种方法?

P.S 我在 Swift 工作,但熟悉 Objective-C - 感谢您的帮助!

最佳答案

根据 Apple 的说法,您应该将 MOC 传递给 Controller ​​。来自核心数据片段。请注意最后两段。

By convention, you get a context from a view controller. You must implement your application appropriately, though, to follow this pattern. When you implement a view controller that integrates with Core Data, you can add an NSManagedObjectContext property.

When you create a view controller, you pass it the context it should use. You pass an existing context, or (in a situation where you want the new controller to manage a discrete set of edits) a new context that you create for it. It’s typically the responsibility of the application delegate to create a context to pass to the first view controller that’s displayed.

A view controller typically shouldn’t retrieve the context from a global object such as the application delegate—this makes the application architecture rigid. Neither should a view controller create a context for its own use (unless it’s a nested context). This may mean that operations performed using the controller’s context aren’t registered with other contexts, so different view controllers will have different perspectives on the data.

Sometimes, though, it’s easier or more appropriate to retrieve the context from somewhere other than application or the document, or the view controller. Several objects you might use in a Core Data-based application keep a reference to a managed object context. A managed object itself has a reference to its own context, as do the various controller objects that support Core Data such as array and object controllers (NSArrayController and NSObjectController in OS X, and NSFetchedResultsController in iOS).

Retrieving the context from one of these objects has the advantage that if you re-architect your application, for example to make use of multiple contexts, your code is likely to remain valid. For example, if you have a managed object, and you want to create a new managed object that will be related to it, you can ask original object for its managed object context and create the new object using that. This will ensure that the new object you create is in the same context as the original.

关于objective-c - 在 Document 的 ArrayController 中访问 appdelegate 的 managedObjectContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24810963/

相关文章:

ios - 带有文本长度计数器的 UITextField

iphone - 为什么我的 UILabel 没有被更改?

objective-c - 如何通过xib将NSMutableArray绑定(bind)到ArrayController

ios - 魔法记录 : crash when fetching data in background thread

ios - 当核心数据文件损坏时如何绕过警报消息?

ios - FSCalendar 日期与其他日期对齐

objective-c - 将两个 XIB 连接到一个 ViewController

ios - 如何在多个用户之间共享核心数据?

objective-c - Cocoa/Objective-C 类中的一致性与继承

objective-c - 访问核心数据中的多对多关系