[Solved] Mogenerator and Xcode 4 advantages/disadvantages

I am somewhat new to core data integration and have found many code samples and tutorials. Some suggest using Mogenerator to generate entity subclasses. Is this an older approach?

Xcode allows one to generate subclasses for entities without a third party tool. Can anyone offer reasons as to why I would choose to use Mogenerator over the embedded Xcode tool for generating entity subclasses?

Solution #1:

This article has a detailed explanation of how to use mogenerator with XCode4, and why you would want to.

In developing your app you’re likely to add your own custom methods to the generated entity classes (NSManagedObject subclasses). But when you run xctool to generate the classes, all your changes are overwritten and lost!

Mogenerator generates a pair of classes for each entity – one stable ‘machine’ class which can be re-generated from the model as you make changes, and one ‘human’ subclass of the machine class which you can edit and add methods to.

I recommend adding a target & build phase to your Xcode project which will generate your entity classes at build time, so you never have to remember to run mogenerator manually.

In addition, mogenerator adds const structs containing the attributes of your entitites to the generated classes, so you can avoid using hard-coded strings in your predicates etc.

Solution #2:

Mogenerator has a real advantage if you have to add instance variables
(or properties backed-up by instance variables) to the managed object subclass.

As David already said, customizing the Xcode generated class file has the big disadvantage
that your changes are lost if you have to re-create the file after modifying the
Core Data entity.

For methods, this can be solved by defining the method in a Category on the managed object
subclass, e.g.

@interface MyEntity (MyMethods)
- (NSString *)myMethod;

However, adding instance variables in a category is not possible (at least not without
using Objective-C runtime tricks such as “associated objects”). And the same applies
to properties if they are backed-up by instance variables.

So if you have that requirement, using mogenerator with the separate class files
is a real advantage. You can add any method, property or instance variable to the
“Human” class that will not be overwritten.

If you don’t need to add instance variables, I don’t see a big difference or advantage
for either solution. mogenerator creates some convenience methods and also structs
containing the property names as strings, but it is your choice if you want to use that
or not.

Respondent: Martin R

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.