How to use a provided store file with Core Data

Apple’s recommended method for dealing with “bring your own store files” for Core Data is to copy the store file into your app’s Documents directory, where it can be accessed for read and write queries.

However, if you don’t need to write to your store file, then you can also add a provided store file inside the main bundle. It would save some space on the user’s device.

Here’s the custom initialiser for NSPersistentStoreCoordinator, tweaked to show how to handle this:

Note that the option NSReadOnlyPersistentStoreOption:@YES is not strictly necessary.

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.

2 thoughts on “How to use a provided store file with Core Data

  1. For completion: if you DO want to copy a provided store file and follow Apple’s guidelines, here’s how to do it. This is the same custom initialiser as above, but with copy/delete routine:

  2. Note that on iOS 7 the SQLite Engine likes to write two additional files: .wal and .shm. While the latter appears to be completely undocumented, .wal files are temporary files used by SQLite which speed up read/write queries. They *should* be deleted at the end of a session, but it appears that they are not on iOS 7.

    Some versions may complain when those files are not present, causing an error message such as “The Disk Image is malformed”. If this happens, just opt to copy the store file instead.

Leave a Reply

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