objective-c - 需要一些帮助来理解 Apple 关于 Core Data 的文档

标签 objective-c cocoa-touch cocoa core-data

我正在阅读苹果的“核心数据片段”文档(https://developer.apple.com/library/mac/#documentation/DataManagement/Conceptual/CoreDataSnippets/Articles/stack.html#//apple_ref/doc/uid/TP40008283-SW1),我对这部分有点困惑。

To create a new managed object context, you need a persistent store coordinator.

NSPersistentStoreCoordinator *psc = <#Get the coordinator#>;
NSManagedObjectContext *newContext = [[NSManagedObjectContext alloc] init];
[newContext setPersistentStoreCoordinator:psc];

If you already have a reference to an existing context, you can ask it for its persistent 
store coordinator. This way you can be sure that the new context is using the same 
coordinator as the existing one (assuming this is your intent):

NSManagedObjectContext *context = <#Get the context#>;
NSPersistentStoreCoordinator *psc = [context persistentStoreCoordinator];
NSManagedObjectContext *newContext = [[NSManagedObjectContext alloc] init];
[newContext setPersistentStoreCoordinator:psc];

最具体的是<#Get the coordinator#>和<#Get the context#>部分。这到底意味着什么以及在实际应用中应该做什么?

谢谢。

最佳答案

根据我的理解,托管对象上下文几乎就像一个暂存器,您可以在其上进行更改,然后将该暂存器保留到存储中。几乎每次您想要将托管对象放入存储时,您都需要它的上下文,因此当您要这样做时,请务必创建一个上下文。我对 PSC 不是很熟悉,但从它的外观来看,你只需要一个,而且这个可以被多个上下文使用。如果您希望在整个应用程序中使用相同的 PSC,那么我想您可以将其放入单例中或以某种方式传递它,这样您就不必每次都创建一个新的。

关于objective-c - 需要一些帮助来理解 Apple 关于 Core Data 的文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12031883/

相关文章:

ios - 在 UIScrollVIew 中确定 UITextView 的滚动偏移量

objective-c - [iPhone SDK] [菜鸟] UITextView textViewDidChange不会被调用?

html - 为什么 WebView 的 dataWithPDFInsideRect 不能在 Mavericks 上创建高质量的 PDF?

objective-c - 24hr至12hr时间转换,将字符插入字符串

ios - 如何使用 CGPoint 获取 UIScrollView 的 subview

iOS,UICollectionViewCell 格式错误的 UILabel

iPhone - 当用户触摸状态栏时,使 UIWebView subview 滚动到顶部(iOS 4)

objective-c - 需要澄清 NIB 文件对象

ios - Objective C (iOS) 中的 VLA 可变长度数组,会发生什么?

objective-c - Xcode 不会警告无效的初始化方法