Вопрос

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.

Это было полезно?

Решение

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.

Другие советы

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].

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top