Sometimes it can be useful to split your Core Data Store File across multiple files. For example, one file could live on the local file system, while the other could live in iCloud.
We can do this by telling the Xcode Model Editor to add more than one Configuration, each of which can be allocated certain Entities. Each Configuration can be configured to use a separate store file.
Consider this example code which is provided by the Xcode 4.6 templates to initiate the Persistent Store Coordinator:
1 2 3 4 5 6 7 |
// Single Store - original code provided with template NSURL *url = [applicationFilesDirectory URLByAppendingPathComponent:@"Two_Stores.storedata"]; NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom]; if (![coordinator addPersistentStoreWithType:NSXMLStoreType configuration:nil URL:url options:nil error:&error]) { [[NSApplication sharedApplication] presentError:error]; return nil; } |
[emember_protected]
Notice the absence of a Configuration in the addPersistentStoreWithType method. You can create Configurations by click-holding the big PLUS button that let’s you add Entities by default. GIve them a meaningful name, then drag-and-drop in your Entities:
Next you’ll replace the code above with something like this, adding more than one store file to your Persistent Store Coordinator:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// create URLs for each store NSURL *url1 = [applicationFilesDirectory URLByAppendingPathComponent:@"Store-One.xml"]; NSURL *url2 = [applicationFilesDirectory URLByAppendingPathComponent:@"Store-Two.xml"]; // create the coordinator NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom]; // add store files to coordinator if (![coordinator addPersistentStoreWithType:NSXMLStoreType configuration:@"Configuration1" URL:url1 options:nil error:&error]) { [[NSApplication sharedApplication] presentError:error]; return nil; } if (![coordinator addPersistentStoreWithType:NSXMLStoreType configuration:@"Configuration2" URL:url2 options:nil error:&error]) { [[NSApplication sharedApplication] presentError:error]; return nil; } |
[/emember_protected]
Now you’ll work with two store files in the same Managed Object Context. This also means that whatever operation you call on the context (save for example) will be executed on both store files.
Demo Project
I’ve added a Demo Project to GitHub which demonstrates this in Mac OS X:
Note that the drag-and-drop functionality currently does not work in the current Xcode 5 Developer Preview 1.
Jay, I’m missing a bit. The example you mention is the seperation of application and user data. I’ve got the models built. I’m generating two sqlite files.But how do I use them so that the app looks like one database? Many thanks
Hi David, it’s an advanced feature of Core Data (which in itself is advanced). I suppose this only makes sense if you know how to create a Core Data app with a single store file. This article only describes how you can add a second store to the same context, not how to actually fetch or save data.
In essence, with the two-stores setup you can talk to Core Data the same way as you would with a single store application: add objects to the same context, specify the configuration – and in the background Core Data takes care of which physical file data is saved to.
For retrieval you’d use two Fetch Requests (one per Entity). This will give you two arrays for your data in a single context. I’m (trying to) explain the basics here: http://pinkstone.co.uk/core-dara-nugget-1-how-to-speak-core-data/