Slow save after changing Core Data relationship validation rule

In Xcode on my data model, I created a new version and just modified a relationship to be optional (instead of non-optional).

Running my updated app with a large database created using previous model version will cause the first save to run really slow. This happens after each app launch!


If I build a large database from scratch using my updated app (means, using new model version) there will be no issue.


Note that I modified the relationship on an entity holding an image data (blob).


Any thoughts?

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:
    1. BEGIN EXCLUSIVE
    2. few
      UPDATE
    3. COMMIT
      (very fast!)
    4. pragma page_count
    5. pragma freelist_count
    6. 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)
Slow save after changing Core Data relationship validation rule
 
 
Q