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:
A. 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.
B. 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).
C. 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 is not necessary and probably not the correct approach)?
Any suggestions?
The solution I used that works well is based on C) above. I leave the NSPersistentDocument and it's persistent store coordinator alone, and instead create my own NSPersistentStoreCoordinator instance that I use exclusively for the persistent library store (global store).
I can set the configuration for the store to a custom value in case I want to have multiple stores associated with this persistent store coordinator later (e.g., "LibraryConfiguration"). Since the library store is managed by a persistent store coordinator different from the NSPersistentDocument's persistent store coordinator, I don't need to be concerned about specifying a configuration for the NSPersistentDocument's persistent stores.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With