Where should I store the source data and convenient methods?

I have a number of convenient methods that fetch for my Core Data objects. For example, a method that returns the current active actions or the time interval between two completed actions or the default client, if any. I also have convenient methods for initializing and adding objects with various attributes.

I have a Singleton model of a data class, which is a go-to class for initializing Core Data and getting NSManagedObjectContext, etc.

Is it better to use these convenience methods in a one-dimensional class of a data model or in each corresponding subclass of an object as class methods? I do not think that there is one true path, but I would like to have opinions and experiences. Thanks!

+3
source share
2 answers

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

Then just put the methods in class files, for example:

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

The general rule that I would give is that if a method works with a specific class, then put the method in that class. =)

+5
source

, , (, FooManagedObject + Convenience.h/.m), NSManagedObject (.. FooManagedObject.h/.m), .

mogenerator, ( ) NSManagedObject , . , , , , . , .

+1

Source: https://habr.com/ru/post/1713496/


All Articles