简体   繁体   English

基于文档的可可应用的多种环境?

[英]Multiple Contexts for Document-based Cocoa Application?

So I'm building a document-based application in Cocoa and trying to understand NSManagedObjectContext. 因此,我在Cocoa中构建了一个基于文档的应用程序,并试图了解NSManagedObjectContext。 I've read Apple's "Core Data Basics" but can't wrap my head around it. 我已经阅读了Apple的“ Core Data Basics”,但无法解决。 In my application I created a custom CoreDataUtility class so that all of my classes can get the context via the following lines of code: 在我的应用程序中,我创建了一个自定义CoreDataUtility类,以便所有类均可通过以下代码行获取上下文:

MyCoreDataUtility *coreData = [MyCoreDataUtility sharedCoreDataUtility];
NSManagedObjectContext *context = [coreData context];

I got this idea from the site "Cocoa is my girlfriend". 我是从“可可是我的女朋友”网站上得到这个想法的。 Where I get confused is when I run my application and I create a new window (my file owner class is a subclass of NSPersistentDocument) if I make changes to the context on the new window, the view reflects the changes on the old window and as well as the new one. 当我对应用程序进行更改并创建新窗口(文件所有者类是NSPersistentDocument的子类)时,如果在新窗口上更改上下文,该视图会反映旧窗口上的更改,而我感到困惑的是以及新的。 Based on this I'm assuming that both windows are referring to the same context or at least the same data in some way. 基于此,我假设两个窗口都以某种方式引用了相同的上下文或至少相同的数据。

So my questions are: 所以我的问题是:

  1. I have the assumption that each time I create a new window a new context is created, is this correct? 我假设每次创建一个新窗口都会创建一个新的上下文,这是正确的吗?
  2. If a new context is created and they are different (I actually printed out the memory address of the two contexts and they addresses are different) does this have something to do with the persistent store coordinator or persistent object store (both of these concepts are also nebulous in my mind)? 如果创建了一个新的上下文,并且它们不同(我实际上打印了两个上下文的内存地址,并且它们的地址不同),这是否与持久性存储协调器或持久性对象存储有关(这两个概念也都是在我脑海中含糊不清)?
  3. If all documents refer to the same context is it my responsibility to create a new context every time a document is created? 如果所有文档都引用相同的上下文,那么每次创建文档时我有责任创建一个新的上下文吗?

Thanks all! 谢谢大家!

I have the assumption that each time I create a new window a new context is created, is this correct? 我假设每次创建一个新窗口都会创建一个新的上下文,这是正确的吗?

No and yes. 不,是的。 Creating a new window does not create a new context, but I suppose that what you see as "a new window" is actually your action of "creating a new document". 创建新窗口不会创建新上下文,但是我想您看到的“新窗口”实际上是您“创建新文档”的操作。 In a document based app with CoreDate you are likely to have a persistent store which creates a new context for every document by itself. 在带有CoreDate的基于文档的应用程序中,您可能会拥有一个持久存储,该存储将为每个文档单独创建一个新的上下文。

That is also the answer to your second question. 这也是您第二个问题的答案。 The store coordinator is just the central point that manages all documents of your application. 商店协调员只是管理应用程序中所有文档的中心点。 It takes care of some menu related actions, such as opening, saving and all side effects. 它负责一些与菜单相关的操作,例如打开,保存和所有副作用。

If all documents refer to the same context is it my responsibility to create a new context every time a document is created? 如果所有文档都引用相同的上下文,那么每次创建文档时我有责任创建一个新的上下文吗?

That is not possible. 这是不可能的。 One context can be related with only one file (or "store"), hence every document must have "at its minimum" one context. 一个上下文只能与一个文件(或“存储”)相关联,因此每个文档都必须“至少”具有一个上下文。 If you are using the template for a CoreData and Multi-document based app, you do not have to worry about creating a new context. 如果您将模板用于基于CoreData和多文档的应用程序,则不必担心创建新的上下文。 As I said before, the store coordinator will take care of that. 正如我之前说过的,商店协调员将负责此事。 However as you learn more about CoreData, and specially if you do multithreading, you will have to create more than one context because because a context cannot cross threads. 但是,随着您了解有关CoreData的更多信息,特别是如果您执行多线程,您将不得不创建多个上下文,因为上下文无法跨线程。

Could you provide the link from Cocoa is my girlfriend where you took that code? 您能提供可可豆是我女朋友的链接吗? It seems you want a singleton to store your context and usually that is a bad idea in a document based app because it is extremely easy to mess up things giving one function the context of a different document. 似乎您想要一个单例来存储您的上下文,这在基于文档的应用程序中通常是个坏主意,因为将一个函数赋予另一个文档的上下文非常容易弄乱事情。 Your application should have a good flow where the context is passed. 您的应用程序应具有良好的传递上下文的流程。 However I always use a singleton if my application is not document based. 但是,如果我的应用程序不是基于文档的,我总是使用单例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM