views:

137

answers:

1

I've got a number of convenience methods that perform fetches for my Core Data entities. For example, a method that returns currently-active activities, or a time interval between two completed activities, or a default client if there is one. I also have convenience methods to initialize and add entities with various attributes.

I have a data model singleton class that is the go-to class for initializing Core Data and getting the NSManagedObjectContext, etc.

Is it better to put these convenience methods in the data model singleton class, or in each relevant entity subclass as class methods? I don't think there's a One True Way here, but I would like opinions and experiences. Thanks!

+3  A: 

I would associate them with the class on which they operate. To do this, I would first generate class files for your entities (select the entities in the editor, then File > New File > NSManagedObject).

Then, just put the methods in the class files, such as:

+ [Activity activeActivities];
- [Activity intervalToActivity:(Activity *)other];
+ [Activity activityWithVariousAttributes]; // (plus maybe a corresponding initWithVariousAttributes)

The general rule I'd give is that if the method operates on a specific class, then put the method in that class. =)

Dave DeLong