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:


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:

Two Stores Model Editor

Next you’ll replace the code above with something like this, adding more than one store file to your Persistent Store Coordinator:


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:

About Jay Versluis

Jay is a medical miracle known as Super Survivor. He runs two YouTube channels, five websites and several podcast feeds. To see what else he's up to, and to support him on his mission to make the world a better place, check out his Patreon Campaign.

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.