Recent Posts

Core Data, a couple of tips

I’ve just submitted my first ever iOS application to the app store, and thought I’d try writing a few blog posts on the topic of iOS development. In this post I’m going to give you a couple of tips, which I think can help you a lot and reduce friction when working with Core Data.

Core Data is a quite a nice ORM-like tool. If you’ve never used Core Data there are lots of Code Data tutorials out there on the interwebs, and you can also check out Apple’s Core Data Programming Guide.

Generating classes from your entity model

XCode has the capability of generating Objective-C classes that represent the entities in your Core Data model, but… Instead I recommend using a tool called mogenerator which generates two classes per entity, one which is a auto-generated class, and one which is editable by you. This allows you to add code to your entity classes, and also allows you to re-create the auto-generated classes (using mogenerator) with little effort and without losing your manual changes.

Querying and updating the model

When working with your model, i.e. querying, updating etc. I recommend using a library called MagicalRecord, which brings ActiveRecord functionality to Core Data. Now I know not everyone likes ActiveRecord, but this library really helps to make your code more clean and elegant. For instance, to query the entity model you need to create a NSFetchRequest instance, which would normally look something like this:

But when using MagicalRecord, it looks like this:

And if you configure MagicalRecord with a ‘default’ NSManagedObjectContext, it’s simply:

MagicalRecord can make your code much cleaner, and a lot easier to refactor too – as you’re now coding against your entity classes rather than strings. There are many more examples of what MagicalRecord can do on its project homepage.

Facebook Twitter Reddit Linkedin Email

  1. Reverse scrolling on Windows (like Mac OS X Lion) Leave a reply
  2. Overload hell in ASP.NET MVC? 3 Replies
  3. CKeditor and ASP.NET 7 Replies
  4. jQuery Confluence Macro 7 Replies