View Full Version : Core Data doc-based but with app-global context?

Oct 19, 2009, 03:13 PM
I'm trying to do something a little more complicated with Core Data than I have in the past and am getting a little confused.

My Core Data Document-based application loads up player and team data from a feed, and builds entities (Player, Team) based on that data. The entities are defined in an xcdatamodel file. OK, so when I put this code into MyDocument (the NSPersistentDocument subclass), this all works great. I've got all my players and teams, everything works with the array controllers and bindings and so forth.

But now the hard part. The problem with that is that each document gets its own managed object context, and so I'm duplicating all these player and team objects (thousands of them) for each document I open. Not efficient. What I would like to do is have all the player and team entities created in one application-global place (context), but then have each document be able to access those entities and use them in their own MOCs. For example, I might choose a Player of the Week in a document, and I'd want to save that with my document, but I would not want to store all the player and team data with each document.

So what is the solution here? Do I have to create my own Managed Object model, context, and persistent store stack manually in my app controller for the player and team data, and then use something like a Configuration to grant access to those entities to each document? Do I create a global MOC and then tell the document to use that MOC instead of its own (but how does that work with saving/opening my documents)?

Any tips would be appreciated, thanks.

Oct 29, 2009, 02:33 AM
Bumping this thread and rephrasing the question in a (hopefully) more simple, more general manner...

What do you do when you need each document in a Cocoa Document-based app have it's own Core Data objects, but you also need the application to have it's own Core Data objects, which are shared and can be accessed from all the open documents?