iosobjective-ccore-datansentitydescription

How to add attributes to an existing Core Data entity (object) programmatically?


I would like to add attributes programmatically to an entity during runtime of my app.

Is this something you would recommend doing or can this lead to issues?

How would I need to combine NSAttributeDescription and NSEntityDescription? I am familiar with creating models using Xcode, but did not do it using NSEntityDescription yet.


Solution

  • It's theoretically possible, but doesn't appear very practical.

    You can modify the NSManagedObjectModel programmatically, as well as NSEntityDescription. Note that -setEntities: (NSManagedObjectModel) and -setProperties: (NSEntityDescription) both trigger exceptions if you modify a model that has been instantiated. So you can't modify your existing model's structure. You'd have to create a new one and copy all of your data from the old Core Data stack to the new one based on your new model..

    Using NSMutableDictionary is a much saner approach.