I am creating multiple persistent store in my application, but I am using only one persistent store coordinator and managed object model. Now my question is when I call save method on managed object context, which persistent store it will use to save the object. So I want to specify the persistent store to be used to save the object. Same while fetching the objects from database, I want to ensure that my fetch query should be executed on a particular store so that I get objects from that store only. Any help?
3 Answers
You may use configurations.
[PersistentStoreCoordinator addPersistentStoreWithType:configuration:URL:options:nil error:]
Say you want to have single managed object context, single managed object model, single persistent store coordinator but two persistent stores, for example first one will be SQLite store and second one will be a in-memory store.
For this setup you create two configurations, "SQLiteStore" for SQLite store and "InMemoryStore" for in-memory store. In XCode (open your .xcdatamodel file):
you see list of available configurations of your managed object model. Managed object model configuration is basically a set of entity descriptions associated with a string name. To add configuration use Editor -> Add Configuration main menu item while you have .xcdatamodel file opened, then type a string name you prefer. Drag entities you want to be stored in first SQLite store to "SQLiteStore" configuration and others to "InMemoryStore" configuration.
Ok, that's it, now it's time to update your code. Go to the scope, where you create persistent store coordinator and add persistent stores to it. The only change is specifying configuration for them:
...
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:yourManagedObjectModel];
NSURL storeURL = … // your store url
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:@"SQLiteStore" URL:storeURL options:nil error:&error])
{
NSLog(@"[Core Data error] Unresolved error %@, %@", error, [error userInfo]);
abort();
}
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSInMemoryStoreType configuration:@"InMemoryStore" URL:nil options:nil error:&error])
{
NSLog(@"[Core Data error] Unresolved error %@, %@", error, [error userInfo]);
abort();
}
...
That's it now, all entities you've dragged to "InMemoryStore" configuration will be automatically saved to in-memory persistent store and the same goes for "SQLiteStore". Maybe you'll have to reinstall your app on the device/simulator after that.
And a fast resume:
- Create Configurations in managed object model editor (.xcdatamodel file);
- In code add several persistent stores to persistent store coordinator, providing appropriate configuration name.
Check this link for more info: http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/CoreData/Articles/cdBasics.html#//apple_ref/doc/uid/TP40001650-SW4

- 6,014
- 3
- 28
- 55
Fetching should not be an issue. The fetch request can be modified to search particular stores using the setAffectedStores: method on an NSFetchRequest.
When you're creating an object, you can assign the entity to a particular store using the assignObject:toPersisentStore: method on NSManagedObjectContext.
As to your question, there isn't really a default mechanism that I am aware of, and it may be that you simply need to set the affected stores to all of your stores:
[request setAffectedStores:[NSArray arrayWithObjects:firstStore,secondStore,thirdStore, nil]];
To be sure that you're looking in all the right places.

- 67,775
- 17
- 69
- 78
-
Hi, I'm facing a similar situation as OP has described. I'm using MagicalRecord and I have one object model, one `NSPersistentStoreCoordinator`, one `NSManagedObjectContext` which is the default one and 2 SQLite `NSPersistentStore`s. One of them acts as permanent store and the other one as a temporary store. Within the app I populate this temporary store. But I get 0 results. I posted an issue on MagicalRecord's [Github](https://github.com/magicalpanda/MagicalRecord/issues/976#issuecomment-77516462). Could you please take a look? – Isuru Mar 06 '15 at 13:24
I think you really want to use a PSC for each store. This will make the problems you describe go away and I can't really see why you would want to have just the one PSC.

- 15,793
- 4
- 51
- 73
-
1I think it should be possible with one PSC. Otherwise the provision of adding multiple stores in a PSC might not be there. – Darshan Prajapati Aug 02 '11 at 17:57
-
I thought the purpose of a PersistentStoreCoordinator was to coordinate multiple persistent stores. Unless they have different Managed Object Models. – daver Aug 30 '11 at 17:02
-
That is what I am using! Is there any issue that would appear if Im loading multiple PersistenteContainers on memory at ones ? – Siempay Nov 15 '19 at 16:42