UPDATE 1:
after adding
-com.apple.CoreData.SQLDebug 1
I figured the following:
- Following the added model version, there is a lightweight migration that rebuilds the image table and re-generate all indexes.
- On the first save this what happens:
BEGIN EXCLUSIVE
- few
UPDATE
COMMIT
(very fast!)pragma page_count
pragma freelist_count
pragma incremental_vacuum(4055)
(very slow, almost 4 seconds!)
pragma incremental_vacuum
is taking almost 4 seconds on iPhone 6 device and much more time on slower devices (more than 10 seconds).
Note that on the first save, no matter which app you develop, steps 1-5 will always take place after app loaded.
Unique for my case is that I get
incremental_vacuum
in addition. I can see why it's invoked (I guess because there are too many unused pages due to rebuilding the large image table in the lightweight migration phase), however, I can't see why it's invoked later on subsequent app loads.
Shouldn't the first invokation of
incremental_vacuum
fix the issue of unused page and update the sqlite file accordingly? It just seems that the result of
incremental_vacuum
is kept in memory and not saved to the file (although file is indeed saved)