views:

32

answers:

1

Hello,

I'm trying to implements some methods for my CoreData models and I'm wonndering if the way I'm doing it is a good practice. I want to implement methods of the type getObjectsWithFieldName. This methods could be used by severals views. So the way I'm doing it is implementing a Class method in the model in the following way:

+(NSArray *)getObjectWithFieldName:(NSString *)fieldName andContext:(NSManagedObjectContext *) context;

Is this a good practice? Or there is another way to do it for iPhone Development?

Thanks in advanced.

+1  A: 

Short answer, probably not. What class were you planning on adding this to?

If you already have access to the NSManagedObjectModel you can query the model directly to get this information. What class were you planning on adding this to?

Update

That is not going to work very well with Core Data because the designs are different. What is your end goal? You are looking for entities that have a particular property, but why? Since you are writing the code you know what the model looks like, why do you need to query the model to look for the entity?

This design, on its face, does not make sense in Cocoa/Core Data development so some clarification is going to be needed.

Marcus S. Zarra
I was thinking in something like the methods that you can find in active_record methods on Rails. For instance ModelName.find(:all) or ModelName.find_by_name("Joe"). So, I impleted the find method in the model and then in ViewController ask the model to do the query for him.But I don't know how to implement this kind of pattern when developing for iPhone.Thanks for your answer!
Rafael
It looks like storing the fetch request in your model will give you what you need. http://www.mactech.com/articles/mactech/Vol.22/22.02/CoreData4/
kubi