1

As the title says, I just add or rename one property of one entity. So, when the user upgrade to the new version, does Core Data have to read all the existing data into memory and write into the destination store?

This is too big a price for just a small change on the data model.

And one more question : Can I use lightweight migration or custom migration dynamically according to the filesize of persistent store? If the database is too big, I run custom migration to avoid memory warning, otherwise I choose lightweight for better user experience.

These confuse me a lot. Thanks for any tips.

Nice links for custom migrations:

example

another example

Community
  • 1
  • 1
Jason Lee
  • 3,200
  • 1
  • 34
  • 71

1 Answers1

4
  1. Changing one property will cause a migration
  2. Renaming a property will be "guessed" at via lightweight migration as a deleted property and then an added property; effectively deleting the values in the property.

I would not recommend a heavy migration ever; it is just too expensive. It is frequently faster to just export to another format and then read it back in.

I would consider not renaming the property but just adding the new property and then in the -awakeFromFetch move the data from the old property to the new one.

Or don't rename the property, what is the code reason for the rename?

Update 1

Yes, you can make a decision on migration. To do that, you ask the store if it needs to be migrated. By calling -[NSManagedObjectModel isConfiguration:compatibleWithStoreMetadata:] you can determine if a migration is needed. From there you can then determine the file size and make your decision.

However, I would not do a custom migration. There are other options:

If you really need to rename the property, consider this:

  1. Check for a migration situation
  2. Migration to an intermediate version where the new property name is added.
  3. Walk your data and copy the values from the old property to the new
  4. Migrate to the final model where the old property is removed.

I am willing to bet that this will be faster than a custom migration and it will consume far less memory since only one copy of the data is resident.

I really don't like heavy weight migrations on iOS :)

Update 2

Thanks to the comment by Alexei Kuznetsov; you can set the Renaming ID on the property you are going to rename to the original name of the property and lightweight migration should migrate the data correctly. Naturally I recommend testing this to make sure there are no surprises.

Marcus S. Zarra
  • 46,571
  • 9
  • 101
  • 182
  • Zarra Soooo happy to see your answer, I've heard your name for such a long time. And thanks for your answer. My real problem is some users have very large datasets, and my app runs out of memory on the migration. So I'm now considering custom migration to handle large datasets. – Jason Lee Dec 20 '13 at 03:08
  • So I add the second question : `Can I use lightweight migration or custom migration dynamically according to the filesize of persistent store?` – Jason Lee Dec 20 '13 at 03:13
  • 5
    When renaming an attribute and using renaming identifier, lightweight migration doesn’t treat it as “delete and add”, but allows to rename an attribute and keep the data, right? – eofster Dec 20 '13 at 11:34
  • 1
    An excellent suggestion @Alexei Kuznetsov! I honestly had not run across that little gem before. Some quick research in the Apple docs confirms that it *should* work. Jason Lee, you should give it a try. – Marcus S. Zarra Dec 20 '13 at 15:41
  • So, @MarcusS.Zarra Do you have any other solutions, when lightweight migration runs out of memory, except custom migration? – Jason Lee Jan 02 '14 at 07:18
  • @Jason Lee: Lightweight migration doesn't run out of memory. Perhaps you are confusing it with a bad food crash or something else? I would suggest opening a new SO question and sending me the link. – Marcus S. Zarra Jan 02 '14 at 16:31
  • http://stackoverflow.com/questions/20896519/crash-on-core-data-migration Here is the link @MarcusS.Zarra thanks – Jason Lee Jan 03 '14 at 04:26