How to use Core Data with multiple Store Files

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:

    // 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;
This content is for members only.

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:

3 thoughts on “How to use Core Data with multiple Store Files

  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

    1. 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:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.