iOS Mogenerator and MagicalRecord

mogenerator_magicalrecord_thumbMogenerator is a very useful command line tool used to generate NSManagedObject subclasses. For a given .xcdatamodel file Mogenerator will generate two classes for each entity. The first class, _MyEntity contains attributes and convenience methods of the entity and will be continuously overwritten to stay in sync with the data model. The second class MyEntity (subclasses _MyEntity) will never be overwritten and is a place to put custom implementation for that entity.

To use Mogenerator with your project, first install it via Homebrew:
$ brew install mogenerator

A convenient way to generate files is to create an aggregate target in your project. After you create the aggregate target, select it and open Build Phases. Click on the “+” button in the upper left corner and select the New Run Script Phase.


Use the Mogenerator example script below to generate the entity class files, adjust it to conform to your data model:

This is what the Build Phases section should look like:


Please note, when you create the CoreData entity classes, make sure that the Class field contains the same name as the Name field.


Now everything is set up to use Mogenerator to generate NSManagedObject subclasses. When you want to generate files, just switch to Mogenerator target and build the project.

MagicalRecord is an extension on CoreData framework that contains convenience methods for creating, updating, deleting and fetching managed objects. It also cleans up the standard CoreData related code.

You can easily add MagicalRecord to your project via CocoaPods.

The setup is very easy, in your AppDelegate import MagicalRecord


and set up the stack in the application:didFinishLaunchingWithOptions: with this one line


Now you can easily create an entity with:


Deleting an entity or all entities:


Fetching entities is very convenient:


You can also check out the sample project at my GitHub repo:



If you liked this blog post I’d appreciate if you followed me on Twitter

Related Post

Remember to share...Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInEmail this to someone

Leave a Reply

Your email address will not be published. Required fields are marked *