简体   繁体   中英

How can I force update in a CoreData SQLite database?

I've got an app, and I use CoreData to store the data. Everything is working fine, but I wanted to easily access to my data, for verification purpose. So I had the idea to try to share the data with iTunes (UIFileSharingEnabled). Now I can see my database in iTunes, I can copy the .sqlite file on my mac, and I can verify the data with sqlite3.

All was OK, but I wanted to test something else, so I modified some datas in my app, and I'm not able to access the modified data. The .sqlite database is always a few days old. I have two other files in iTunes sharing (.sqlite-shm and .sqlite-wal), which are modified (the dates change when I modify the data), but I can't 'update' the .sqlite file.

I tried force-quitting the app on the iPhone, rebuilding the app, creating new datas instead of modifying, with no luck.

The data in my app is correct, so I assume that the modifications are stored in the two other files, but does anybody know what I can try to force the update of the .sqlite file?

The other files exist because the store uses the SQLite journaled type. You simply need to use that type when initialising your sqlite3 tool.

Ideally you'd use the app code only to interrogate the data store instead of using sqlite3 as the table structure should be considered private.

According to Technical Q&A1809 , the way to force SQLite to merge the -wal file into the main .SQLite file is to use "rollback journaling mode" to add the store to the persistent store coordinator:

For a store that was loaded with the WAL mode, if both the main store file and the corresponding -wal file exist, using rollback journaling mode to add the store to a persistent store coordinator will force Core Data to perform a checkpoint operation, which merges the data in the -wal file to the store file. This is actually the Core Data way to perform a checkpoint operation.

The technical note includes sample code to achieve this (albeit Objective C).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM