Core Data Document-Based Application with Global Persistent Store
Core Data Document-Based Application with Global Persistent Store
- Subject: Core Data Document-Based Application with Global Persistent Store
- From: Dalmazio Brisinda <email@hidden>
- Date: Wed, 17 Nov 2010 21:32:37 +0700
I've got a document-based Core Data application which works as is. I would like to add support for a global persistent store to hold a library of items.
I've read most of the relevant docs, and understand that I should use configurations in the managed object models. I've defined two configurations: "DocumentConfiguration" and "LibraryConfiguration". The entities in the document configuration are only in the document configuration, and the entities in the library configuration are only in the library configuration -- i.e., no overlap.
The docs then say "You then use this model when you create a coordinator". But I don't actually create my own persistent store coordinator since I'm using the default NSPersistentDocument coordinator.
A few questions on how best to proceed and help clear up any misunderstandings I might have:
1. Would I obtain the NSPersistentStoreCoordinator in the NSPersistentDocument and then add a new persistent store to it along the lines of:
NSPersistentStoreCoordinator * coordinator = [[myDocument managedObjectContext] persistentStoreCoordinator];
[coordinator addPersistentStoreWithType:NSXMLStoreType
configuration:@"LibraryConfiguration"
URL:url
options:nil
error:&error];
?
I'm thinking that this may be a problem because I haven't provided the other configuration definition ("DocumentConfiguration") in the NSPersistentDocument's persistent store coordinator as I'm using the default provided by NSPersistentDocument. I'm guessing it would probably use nil when the time came to save the document. And if so, would this be a problem? I.e., how would the coordinator know which persistent store to save an entity with a given configuration definition if the same configurations are not defined for all the persistent stores (in this case two)? Am I able to set the configuration (to "DocumentConfiguration") of the NSPersistentDocument's persistent store before it has been created/saved? From the NSPersistentDocument docs:
"Saving a new document adds a store of the default type with the chosen URL and invokes save: on the context. For an existing document, a save just invokes save: on the context."
2. Would it be better to create my own NSPersistentStoreCoordinator and NSManagedObjectContext instances, adding the two persistent stores with configurations defined, and then make the NSPersistentDocument use these NSPersistentStoreCoordinator and NSManagedObjectContext instances, and free the old ones? If so, how would I specify the url for the NSPersistentDocument for the addPersistentStoreWithType:... method? It seems this URL is only known once the untitled document has been saved. (Testing this, there does not appear to be any temporary persistent store (via method persistentStores on the persistent store coordinator) until the document is saved for the first time).
3. Or would it be better to leave NSPersistentDocument alone, and create my own NSPersistentStoreCoordinator instance that I use exclusively for the persistent library store and managed library object model? The docs say that multiple instances of NSPersistentStoreCoordinator should be used in multithreaded Core Data applications, but I don't require multithreaded Core Data support. Is it desirable to have two instances of NSPersistentStoreCoordinator -- one for the library and one for documents (intuition says that this not necessary and probably not the correct approach)?
Any suggestions?
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden