Frage

Question regarding Core Data migrations:

The original model was just a single entity (Transaction) with multiple properties. The app could store N Transactions, and display them in a table.

Original Core Data Model (simplified):

Transaction:
    property_x
    property_y
    property_z

The model has now been changed so that the new top-level entity is a TransactionsList entity, with a 1-to-many relationship so that each TransactionsList has multiple Transactions.

New Core Data Model (simplified):

TransactionsList:
    property_a
    property_b
    transactions <-->> Transaction:
                                property_x
                                property_y
                                property_z
                             <- transactionList

The model has now been changed so that the top-level entity is a TransactionsList entity, with a 1-to-many relationship (transactions) so that each TransactionsList has multiple Transactions.

I'm having a problem finding any examples (either in Apple docs, or on the web) of how to set up a migration from the original configuration to this new configuration.

Lightweight migrations don't seem to work (since this change involves adding a new relationship).

I set up a mapping file, and tried to create a custom migration policy, but if I set breakpoints on any of the functions in it, they are never hit.

I turned on migration debugging, and it indicates that the migration succeeded:

"Automatic schema migration succeeded for store at "..."

However, if I look at the internals of the sqlite database, there are no database entries for the TransactionsList entity, and all the transactionList fields in the Transactions (which should be linked to the owning TransactionsList) are null.

Two questions...

1) Any clues as to why my custom entity migration policy might not be getting called

2) Any suggestions on the best way to set up this kind of migration?

Thanks.

War es hilfreich?

Lösung

You are correct that a light-weight migration will not work. Further, a heavy migration would be very expensive computationally and I would not advise it.

I recommend a two step approach:

  1. Allow a light-weight migration to occur. This will cause your TransactionList entity to be empty.
  2. After the migration, walk through your transactions and create TransactionList entities as appropriate.

This will be faster and easier to test/debug than using a heavy migration. You can test to see if a migration needs to occur using the -[NSManagedObjectModel isConfiguration: compatibleWithStoreMetadata:] method. This will let you know a migration is needed, then you kick off the migration and finally do the post processing.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top