4

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

Fábio Oliveira
  • 2,346
  • 21
  • 30
  • 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 - https://github.com/halostatue/coredata-easyfetch - which also helps to keep things simple :) – André Morujão May 04 '12 at 08:09
  • 1
    Seems like it's current state of matters, confirmed here: http://www.sunetos.com/items/2011/07/24/how-to-use-custom-classes-with-core-data-without-fear/ – Piotr Byzia Aug 02 '12 at 15:41
  • @piobyz it's great to see all the alternatives explained so people understand the tradeoff. The only thing mission IMO is referring this [link](http://www.cimgf.com/2011/03/13/super-happy-easy-fetching-in-core-data/) because it also takes a lot of work when you're doing Core Data related code. – Fábio Oliveira Aug 03 '12 at 13:51

1 Answers1

2

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.

Community
  • 1
  • 1
Amogh Talpallikar
  • 12,084
  • 13
  • 79
  • 135