Backup .sqlite (master data)

I have a basic data-based application that uses Dropbox to back up and restore data. The backup method is quite simple. I copy the .sqlite file to the user's Dropbox.

Now my backup and restore functions work fine. The problem is with the .sqlite file itself. The .sqlite file seems to be incomplete .

I entered about 125 entries in my application and made a backup. The backup appeared in my Dropbox, but when I use the .sqlite explorer tool to view the contents, I only see records up to the 117th record.

I tried updating the first record, and then looked at the .sqlite file again, but there were no changes again.

What's even weirder is that the application seems to have recorded all the changes. When I add a new record or update an existing one and restart the application, the new data added seems to be saved. But this recently added data does not appear in my .sqlite file.

I support this code:

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; NSString *filePath = [[[appDelegate applicationDocumentsDirectory] path] stringByAppendingPathComponent:@"MyApp.sqlite"]; if (account) { if ([filesystem isShutDown]) { filesystem = [[DBFilesystem alloc] initWithAccount:account]; [DBFilesystem setSharedFilesystem:filesystem]; } DBPath *newPath = [[DBPath root] childPath:[NSString stringWithFormat:@"Backup - %@.sqlite", [NSDate date]]]; DBFile *file = [[DBFilesystem sharedFilesystem] createFile:newPath error:nil]; [file writeContentsOfFile:filePath shouldSteal:NO error:nil]; [filesystem shutDown]; } 

I also copied the .sqlite file from the Simulator folder and tried to see it in the .sqlite browser. He still exhibits the same behavior. Any reason why this should happen?

+7
database ios sqlite objective-c core-data
source share
1 answer

Starting with iOS 7 / OS X 10.9, Core Data uses the โ€œWrite-Ahead Loggingโ€ (WAL) record by default for logging mode for the main SQLite repository file. This is explained in

Q & A QA1809: New default logging mode for Core Data Warehouses in iOS 7 and OS X Mavericks

In WAL mode, the main data keeps the main storage file intact and adds transactions to the -wal file in the same place. After the Core Data Context is saved, the -wal file is not deleted, and the data in this file will also not merge the storage file. Therefore, simply making copies of the storage file is likely to result in data loss and inconsistency.

This should explain why your .sqlite file is incomplete. Like the solution, you can (also explained in this technical note):

  • Disable WAL mode (and use the "old" rollback logging mode) for SQLite repository by setting

     @{NSSQLitePragmasOption:@{@"journal_mode":@"DELETE"}}; 

    when adding persistent storage or

  • Use

     - (NSPersistentStore *)migratePersistentStore:(NSPersistentStore *)store toURL:(NSURL *)URL options:(NSDictionary *)options withType:(NSString *)storeType error:(NSError **)error 

    to back up the master data store.

+6
source share

All Articles