ios - Backing up .sqlite (Core Data) -
i have core data based application uses dropbox backup , restore data. way backup pretty straight forward. copy .sqlite file on user's dropbox.
now backup , restore functionality working fine. issue .sqlite file itself. appears .sqlite file incomplete.
i entered 125 entries in application , took backup. backup appeared in dropbox when use .sqlite explorer tool see contents, see records upto 117th entry.
i tried updating first entry , again observing .sqlite file no changes again.
what's more strange app appears have recorded changes. when add new entry or update existing 1 , restart app, newly added data seems persist. newly added data not appear in .sqlite file.
i backing using 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 copied .sqlite file simulator's folder , tried seeing in .sqlite browser. still exhibits same behaviour. reason why must happening?
starting ios 7 / os x 10.9, core data uses "write-ahead logging" (wal) default journaling mode underlying sqlite store file. explained in
technical q&a qa1809: new default journaling mode core data sqlite stores in ios 7 , os x mavericks
with wal mode, core data keeps main store file untouched , appends transactions -wal file in same location. after core data context saved, -wal file not deleted, , data in file not merged store file either. therefore, making copies of store file cause data loss , inconsistency.
that should explain why .sqlite file alone incomplete. as solution can (also explained in technical note):
disable wal-mode (and use "old" rollback journaling mode) sqlite store setting the
@{nssqlitepragmasoption:@{@"journal_mode":@"delete"}};option when adding persistent store, or
use the
- (nspersistentstore *)migratepersistentstore:(nspersistentstore *)store tourl:(nsurl *)url options:(nsdictionary *)options withtype:(nsstring *)storetype error:(nserror **)errormethod make backup copy of core data store.
Comments
Post a Comment