Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding attribute to entity in Core Data

I've created all of my Managed Objects after mapping out all entities/attributes/relationships in the data model. Now I have the problem of needing to add additional attributes/relationships I haven't thought of when I first designed one of my entities/classes. Is there a way to modify my existing NSManagedObject class with Core Data short of wiping all of my models and re-creating them based on the new xcdatamodel?

Will adding the attribute in the xcdatamodel update the underlying storage mechanism also? Say if I'm using SQLite3 as my persistent storage, will it add the column accordingly?

like image 705
Coocoo4Cocoa Avatar asked Aug 03 '09 01:08

Coocoo4Cocoa


People also ask

What is attribute in Core Data?

You can think of attributes as the columns of a table in a database. Attributes store the values of a Core Data record. There are several types of attributes, such as String, Date, Integer, Float, and Boolean.

How do I add fetch index elements in Core Data?

You'll see a small + button under Fetch Index Elements in the main editor – click that to add a new index element, then change its property from “Expression” to “name”. An indexed attribute is one that is optimized for fast searching.

What is an entity in Core Data?

An entity describes an object, including its name, attributes, and relationships.


2 Answers

As groundhog points out, for complex changes to your data model you'll need to create versions of your model and migrate data under the older model into the new one, following Apple's guide on the matter (which he links to). Don't worry about any of the behind-the-scenes SQL, Core Data handles that for you.

However, for simple data model changes, Apple has introduced a new feature in the iPhone OS 3.0 implementation of Core Data called lightweight migration. For lightwight migration, Core Data will automatically migrate across simple changes in your data model, such as changing the name of an attribute or entity, deleting an attribute, adding an attribute with a default value, or changing the inheritance of an entity. You just need to enter in the renaming identifier in the new version to point to the older version's name for something, etc. Core Data will handle the updates of your data in an efficient manner, as long as you set the NSMigratePersistentStoresAutomaticallyOption and NSInferMappingModelAutomaticallyOption options on your persistent store.

like image 119
Brad Larson Avatar answered Sep 18 '22 12:09

Brad Larson


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

However, if you mean you need to migrate an existing store to a new schema, you have some work ahead of you. There are certain conditions (adding orthogonal entities, fetched properties, etc) that won't force you to do this. You will realize if you need to or not when you try to add your existing persistent stores to the persistent store coordinator for your managed object context.

Before you decide to embark on doing a schema change, you should always read up on how to do migrations and versioning in Core Data - if you have existing stores you need to retain. This is almost assuredly the case in apps that have user data stored in Core Data stores. And unless you have an automated import tool or data store generation utility your existing static stores are also likely to need migration.

like image 44
groundhog Avatar answered Sep 18 '22 12:09

groundhog