Is Core Data migration needed when a new attribute is added to an existing entity? [closed]
Asked Answered
S

1

6

I have read a few articles indicating that there should be light weight migration whenever there is a change in entity, attribute or relationship. But a few days ago when I was talking to my senior he asked me if lightweight migration is really required when a new attribute is added. This got me confused. I know that we should always create a new version before making any changes in the Core Data model. So does it mean that versioning is the same as migrations. Do we need migration if we rename an existing attribute. I would really appreciate if someone could help me.

Surety answered 3/2, 2021 at 5:57 Comment(2)
My opinion on your 3 questions, Question 1: Yes (you need light weight migration as coredata can infer the changes such as adding, removing or renaming the attribute of an entity) Question 2: yes, explanation is same as that for question 1 Question 3 : Nope, they are not. Migration can be of 2 types lightweight migration (where coredata infers the changes and automatically performs migration when requested), heavy weight migration, when u realize that capability of coredata to infer changes is not enough to cope with your changes and need to partially or completely handle manual migrationHerbivorous
read : developer.apple.com/documentation/coredata/…Herbivorous
S
9

Do we need migration when we add a new attribute to an existing entity?

A migration is required but you do not created it on your own. Adding a property is perfectly covered by automatic lightweight migration. When you add a property to the core data schema and open an existing store with lightweight migration enabled the existing store will seamlessly be migrated.

As a rule of thumb, when adding new entities or properties to existing entities you're fine with automatic lightweigth migration. For type changes and renaming of existing properties you most probably need to create a heavyweight migration or data will be lost when opening existing stores with the new schema.

Is migration and versioning the same thing

No. Versioning refers to the core data schema while migration is applied to an existing store. You should add a new schema version when you're about to change existing entities or relationships. Furthermore it's important to add a new schema before changing anything in the schema when you had created a heavyweight migration with this schema as target. Otherwise the migration will break because it has no matching target anymore.

Senhor answered 3/2, 2021 at 8:52 Comment(2)
Thanks for your response!! Doesn't renaming the attribute require a mapping model?(as mentioned in apple docs) and writing mapping models means it is a heavy weight migration, doesn't it?Surety
Yes, a mapping model is a key part of a heavyweight migration. Mind the gap: if you ever create a mapping model for any given destination schema you must ensure to not alter this schema anymore or it will break the mapping model.Senhor

© 2022 - 2024 — McMap. All rights reserved.