1
votes

I'm reading Apple's "Core Data Snippets" document (https://developer.apple.com/library/mac/#documentation/DataManagement/Conceptual/CoreDataSnippets/Articles/stack.html#//apple_ref/doc/uid/TP40008283-SW1), and I'm a bit confused at this part.

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];

Most specifically the <#Get the coordinator#> and <#Get the context#> parts. What exactly does that mean and what should go there in an actual application?

Thanks.

1

1 Answers

1
votes

From my understanding, the Managed Object Context is almost like a scratchpad on which you make your changes, and then persist that scratchpad to storage. Pretty much every time you want to put a managed object in to storage, you need a context for it, so always make one when you're about to do so. I'm not very familiar with the PSC, but from the looks of it, you would only need one, and that one can be used by multiple contexts. If you want the same PSC to be used throughout your application, then I guess you can put it in a singleton or pass it around somehow so you don't have to make a new one every time.