part of the groupelephant.com family
beyond corporate purpose

Stop worrying about Core Data

10 Dec 2010

Core Data is one of the frameworks I find a bit scary to use when I’m developing iOS applications. I always felt like I was developing something that wasn’t really under my control. That was until Piet pointed out this framework to me: Mogenerator.

Draw your models

The first thing you should do is create your models in the XCode Modelling Tool. More information on this topic can be found here.

Here is an example of my Core Data model:

Core Data model

Let’s generate

When you created a model with some attributes, you can use mogenerator to generate your classes (or regenerate them, when you did some editing).

I pass two arguments to mogenerator to generate the files.

  • -m the location of your data model
  • -O the location of your model classes

Here is an example:

mogenerator -m TestProject.xcdatamodel -O Classes

This command generates all the classes for your models created with the modelling tool. Be aware: You still have to add them to your XCode project in orde to use them.

What is generated?

For each model four class files are generated. The 2 class files starting with a dash should never be modified. When you want to add custom methods/logix, you can do this in the other 2 generated classes. They extend from the dashed-classes.

Generated Files

Every attribute can now be accessed as a property on the object. This makes it very easy to assign new values.

Don’t forget that when you change your model in the modelling tool, that you’ll have to run the mogenerator command again in orde to regenerate the dashed files.

Some code

The code snippet below will create a Person object and assigns some values to the attributes. When everything is assigned the changes are saved.

// NSManagedObjectContext from somewhere in your system
NSManagedObjectContext *moc = ...

Person *p = [Person insertInManagedObjectContext:moc];

p.firstName = @"Zot";
p.lastName = @"Franske";
p.age = [NSNumber numberWithInt:25];

[moc save:nil];

Check these out!

Here are some links that helped me during development:

blog comments powered by Disqus