views:

69

answers:

1

Hi! can anyone guide me how to create a fetch request that will query an entity and returns any properties that qualify my criteria.

Here's what I have. I have an entity that has 35 properties, all are in types of float. What I need was to see all properties of the entity which values was <= zero.

I know how to return the values of the properties but not how to return the name of the property.

Thanks,

A: 

CoreData doesn't return properties. It returns entities, which then have properties. In any case, you'd have to do something like this:

(The following is pseudo-code done from memory. Treat it accordingly.)

NSString *query = @"(property1 <= 0) && (property2 <= 0)";
NSPredicate *predicate = [NSPredicate predicateWithFormat:query];
NSEntityDescription *entity = [NSEntityDescription entityDescriptionForName:@"Entity" inManagedObjectContext:context];
NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
[fetch setEntity:entity];
[fetch setPredicate:predicate];
NSError *error = nil;
NSArray *entities = [context executeFetchRequest:fetch error:&error];
// Let's just assume we got one
NSManagedObject *obj = [entities objectAtIndex:0];
float value = [obj valueForKey:@"property1"];

Or something like that.

Gregory Higley