Reputation: 933
Let's say I have a single model but I want to save objects to more than 1 NSPersistentStores. Assuming I use a single NSPersistentStoreCoordinator to manage those stores, what are the pros and cons of this setup?
Upvotes: 1
Views: 299
Reputation: 688
With regard to the original question (multiple stores, one coordinator):
The potential advantage of two stores under one PSC is that you can spread out the data between two sqlite files (assuming that's the store type you're using). This can be helpful for example if you want to ship a pre-populated sqlite file with your app (which you can easily update in subsequent releases by shipping a new file), while still having user-created data next to it.
The disadvantage is that handling relationships across different stores is more cumbersome than within one store.
With regard to another answer to this question (multiple coordinators):
There certainly are potential benefits of using multiple coordinators with the same store, primarily performance related. Any request to the coordinator will lock it, so that everybody else has to wait to fetch data or save data. By using two coordinators you can push the lock down to the sqlite file, where it comes and goes much quicker.
Furthermore, a sqlite store that uses write-ahead-logging instead of a rollback journal enables multiple reader, single writer access to the store. By using two coordinators you can take advantage of sqlite's concurrency abilities. Apple is using this pattern internally too.
See also Apple's WWDC 2013 session on Core Data and iCloud (can't link because of ongoing dev-center outage...)
But keep in mind that all this is pretty esoteric and not necessary in almost all cases.
Upvotes: 3