4
votes

I've been dealing with Core Data for the first time and I wanted to know what the best practices are for extending the classes that Xcode generates for my NSManagedObject entities.

I saw mogenerator and I've been also using a similar approach to that as suggested in SUPER HAPPY EASY FETCHING IN CORE DATA. So I had three kinds of classes:

  • The EasyFetching category (only one class);
  • A generated NSManagedObject subclass (i.e.: _Entity);
  • A custom subclass with some custom methods like finding all the inactive objects, clearing the cache for an object, etc. (i.e.: Entity).

This approach let me do some custom code while I could refactor my Core Data entity and generate it as many times as I needed. But I've also run into some problems like not being able to declare object level methods for my entities (because the NSManagedObjectContext only knew my _Entity classes).

Now I'm using categories to extend my entities functionalities. And this works a lot better because I can have custom object level methods. I now have three kinds of classes:

  • The EasyFetching category (as it has a lot of methods that all my custom code uses);
  • A generated NSManagedObject subclass (i.e.: Entity);
  • A custom category for my NSManagedObject entity (i.e.: Entity+Custom.h).

My question is: what would you recommend?

Thanks in advance for your answers

1
That's basically what I've been doing as well: custom categories for my Core Data entities with a few extra methods and such. As for the EasyFetching category, I'm actually using something based on this - github.com/halostatue/coredata-easyfetch - which also helps to keep things simple :)André Morujão
Seems like it's current state of matters, confirmed here: sunetos.com/items/2011/07/24/…Piotr Byzia
@piobyz it's great to see all the alternatives explained so people understand the tradeoff. The only thing mission IMO is referring this link because it also takes a lot of work when you're doing Core Data related code.Fábio Oliveira

1 Answers

2
votes

Now that you have posted your question as an answer on my question, I thought I should answer your question :)

Mogenerator doesn't look bad, give it a try.

Also the way you suggested with categories is also a fine option.

Infact here is a link that exactly explains How to do so.