Technical Q&A QA1809

New default journaling mode for Core Data SQLite stores in iOS 7 and OS X Mavericks

Q:  My app backs up and restores a Core Data SQLite store by copying the store file. This works well on iOS 6.x and OS X Mountain Lion, but fails on iOS 7 and OS X Mavericks after updating to the latest SDK. Why is that?

A: The failure occurs because the default journaling mode for Core Data SQLite stores was changed to Write-Ahead Logging (WAL) in iOS 7 and OS X Mavericks. With the WAL mode, Core Data keeps the main store file untouched and appends transactions to a -wal file in the same location. After the Core Data context is saved, the -wal file is not deleted, and the data in that file is not merged to the store file either. Therefore, simply making copies of the store file will likely cause data loss and inconsistency.

If you create a Core Data stack from a store file which is generated by copying the main store file but not the corresponding -wal file, your app can fail in a number of ways, including:

To safely back up and restore a Core Data SQLite store, you can do the following:

For more information about the default journaling mode change, please see WWDC 2013 session 207 What's New in Core Data and iCloud.

NOTE: In iOS 6.x and Mountain Lion, the default is rollback journaling mode, in which Core Data creates a -journal file to temporarily store transactions, updates the main store file in place and deletes the -journal file after saving the context. The store file therefore contains the up-to-date database.



Document Revision History


DateNotes
2014-01-29

New document that describes the change to the default journaling mode for Core Data SQLite stores and the potential impacts.