Extending Core Data generated NSManagedObject classes

ε祈祈猫儿з 提交于 2019-11-30 17:23:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!