问题
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:
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.
来源:https://stackoverflow.com/questions/10427340/extending-core-data-generated-nsmanagedobject-classes