문제

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