Reputation: 26506
I am frequently setting up fetch requests in multiple classes to retrieve 'allRecipes
', or a 'lastModifiedDate
' from core data (plus a few other results).
It would be convenient to use a specialized category, NSManagedObjectContext+RecipeAppConveniences
, for this. I don't really need to make this generic, I will just decide on a case by case basis if I am fetching the same thing frequently enough that it would be useful in the category.
Is this a standard practice? I haven't used categories much and just wanted to check it's not an anti-pattern, and that I'm not overlooking something that will cause me pain down the line.
Upvotes: 0
Views: 132
Reputation: 30727
The Apple way is to add the method to a NSManagedObject subclass that takes the NSManagedObjectContext as a param, e.g.
@interface Recipe : NSManagedObject
+ (NSArray *)allRecipesInContext:(NSManagedObjectContext *)moc;
@end
This pattern also aligns better with the new API for inserting new objects, e.g. [Recipe.alloc initWithContext:moc]
.
Upvotes: 1
Reputation: 46563
It is not going to be an anti-pattern, if you think that will make your code generic, readable you can go for it.
It was added to objective-c just for this flexibility.
Upvotes: 1