Brian
Brian

Reputation: 3601

NSManagedObjects with Categories

I believe it is a common practice to put custom code for a NSManagedObject in a category of the subclass since Xcode will overrite your generated subclass when the model is edited. I just wanted to confirm this. I have seen examples where people say it is bad to combine categories with methods that are already implemented in the class hierarchy. I'm not sure if this is just for the cases when the class that has the actual category has the method already implemented or in all cases.

Upvotes: 2

Views: 1837

Answers (1)

Andrew Madsen
Andrew Madsen

Reputation: 21383

The problem with overriding a method in a category is that you can't call the original implementation like you'd normally do using [super doSomething]. Usually, when you override a method, you still want to be able to call the original implementation doing something extra before and/or after the original implementation gets executed.

One fairly clean solution is to let Xcode generate NSManagedObject subclasses that you don't touch directly. Instead, create another, custom subclass of each Xcode-generated NSManagedObject subclass that you can edit without worrying about it being overwritten.

Wolf Rentzsch's mogenerator essentially uses this approach, and additionally generates some nice convenience methods that Xcode doesn't generate. Might be worth checking out because it can be a helpful tool, but mogenerator is not necessary to do what you're trying to do.

Upvotes: 3

Related Questions