For clarity, using Xcode 7.2.1, SQLite data store, Core Data object graph, for a prototype app.
My problem was detailed by the Xcode terminal as:
CoreData: error: (11) Fatal error. The database at
/Users/etc/Library/Developer/CoreSimulator/Devices/etc/data/Containers/Data/Application/etc/Library/Application Support/com.etc.etc/etc.sqlite is corrupted.
SQLite error code:11, 'database disk image is malformed'.
Effectively my app was able to load and read the SQLite data, but was unable to save.
This answer by SO user software evolved made sense to me. While using Simulator I was fairly certain I had interrupted a save operation on a managed object context with private queue concurrency type NSPrivateQueueConcurrencyType.
Further investigation (using SQLiteManager) revealed the SQLite table I had been saving to at the time was the cause of this problem.
I could have easily deleted the app (no public release yet) however I wanted to understand at least how to repair this problem.
Notes from this experience:
- In the app delegate under the UIApplicationDelegate Protocol
- (void)applicationWillResignActive:(UIApplication *)application, if your managed object context hasChanges be sure to include a database save method;
- If using more than one queue, use the Home button to trigger the delegate method in item 1 before clicking the stop button in Xcode;
- Repair the damaged database file - I developed a solution detailed below from the answer outlined on this webpage Fixing the SQLite error “The database disk image is malformed”.
SQLite Database File Repair Method:
- Open terminal [terminal/sqlite commands];
- Navigate to the appropriate file location [
cd];
- For backup purposes, make a copy of the 'malformed' database file (e.g.
dbMalFormedBU.sqlite) (that can be deleted later if the repair is successful) [cp];
- To be certain, delete the
dbMalFormed.sqlite-shm and dbMalFormed.sqlite-wal files [rm];
- Open your 'malformed' database file [
sqlite3 dbMalFormed.sqlite];
- Clone your database file [
.clone dbMalFormedNew.sqlite];
- Exit SQLite3 [
.exit];
- Delete the old 'malformed' database file [
rm dbMalFormed.sqlite);
- Rename the new database file to the name used previously [
mv dbMalFormedNew.sqlite dbMalFormed.sqlite].