Adding an Attribute to an Object in Core Data

I created all my managed objects after displaying all entities / attributes / relationships in the data model. Now I had the problem of having to add additional attributes / relationships that I did not think about when I first developed one of my entities / classes. Is there a way to modify an existing NSManagedObject class using Core Data that does not allow me to clear all my models and recreate them based on the new xcdatamodel?

Will an attribute be added in the xcdatamodel update of the underlying storage engine? Say, if I use SQLite3 as my persistent store, will it add the column accordingly?

+6
iphone cocoa core-data
source share
3 answers

For reasons of severity, for complex changes to your data model, you will need to create versions of your model and transfer the data in the old model to the new one, following Apple's guide to this issue (to which it refers). Don't worry about any of the backstage SQL, Core Data handles this for you.

However, for simple changes to the data model, Apple has introduced a new feature in iPhone OS 3.0 for Core Data called lightweight migration . For migration, lightwight Core Data is automatically transferred through simple changes to your data model, such as changing an attribute or object name, deleting an attribute, adding an attribute with a default value, or changing object inheritance. You just need to enter the rename identifier in the new version to point to the older version name for something, etc. Master data will effectively process updates to your data if you set the NSMigratePersistentStoresAutomaticallyOption and NSInferMappingModelAutomaticallyOption parameters to your permanent store.

+9
source share

If you mean "can I change my xcdatamodel and just merge the changes from the generated code into my existing code for the derived NSManagedObject classes," yes, it's simple. You simply generate code for models that have changed, and then merge the changes manually into these specific derived classes. Since the changes sound like they are just additional attributes and relationships, this should be trivial - in fact, you can use diff and patch to do this semi-automatically if your changes are really additive in nature.

However, if you mean that you need to transfer the existing store to a new scheme, you have some work ahead. There are certain conditions (adding orthogonal objects, extracted properties, etc.) that will not force you to do this. You will understand what you need or not when you try to add existing persistent storage to the permanent storage coordinator for your managed entity context.

Before you decide to start changing the schema, you should always read how to do migrations and version control in Core Data - if you have existing stores that you need to save. This is almost certainly the case in applications that store user data stored in Core Data repositories. And if you don't have an automated utility to import or create a data warehouse, your existing static stores also probably need to be migrated.

+8
source share

You should also check out the Generation Gap design templates. This will help you in such a situation. Here is the issue of using generation gap with CoreData.

0
source share

All Articles