Question

I have a basic notes app and I'm learning to work with Core Data, so what I learned is that now all of the models that are part of my MVC, like a Note.h and Note.m that have all the data of a note will now be created through the data model file of Core Data through entities. So I have created a Note entity, I gave it an NSString content attribute and created a NSManagedObject subclass (like they teach in the Stanford videos), but I'm wondering if now I want to add a new attribute to Note model how would I do that? Should I manually add it in the data model file of Core Data and than manually to the Note.h and .m files? this creation through the editor got me confused a bit.

Was it helpful?

Solution

After changes to the Core Data entities, you must recreate the managed object subclass files via "Editor -> Create NSManagedObject Subclass …" from the Xcode menu. Manually editing these files only bears the danger of introducing errors.

Alternatively, use tools like "mogenerator" to create and update the files automatically. See (for example) Mogenerator and Xcode 4 advantages/disadvantages for a comparison between "mogenerator" and the built-in Xcode file generation.

OTHER TIPS

You can achieve it in different ways...The simplest way is to add your attribute to your entity and then 1) manually insert a new property to your Note subclass or 2) use Xcode to do it for you.

If you follow the second way, using Editor -> Create NSManagedObject Subclass …, Xcode will ask you to overwrite existing files. So if you have performed customization on those files they will disappear...

My personal taste. I prefer the first way. The manually one.

An important note is the following. If you have already an app that has already set up a persistent store, and then you change the model, when you will relaunch the app it will crash since there is a difference between the two models. Solutions are or deleting the application and run it again or use light migration. e.g. how-to-perform-a-lightweight-core-data-migration

Yes, you should manually add it in the data model file of Core Data and than manually to the Note.h and Note.m files.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top