- (NSManagedObjectContext *)anObjectByEntityForName:(NSString *)entityName withValue:(NSObject *)value forKeyPath:(NSString *)keyPath {
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:entityName inManagedObjectContext:self.managedObjectContext]];
[request setPredicate:[NSPredicate predicateWithFormat:@"%@ == %@", keyPath, value]];
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[self.managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (!mutableFetchResults) {
[request release];
[mutableFetchResults release];
return nil;
}
if ([mutableFetchResults count] == 0) {
[request release];
[mutableFetchResults release];
return nil;
}
id anObject = [mutableFetchResults objectAtIndex:0];
[request release];
[mutableFetchResults release];
return anObject;
}
This code returns nil, for a keypath "isSelected" and value @YES. But in case there is no predicate, then all the objects are returned. In the database there are minimum 1 object that meets the criteria. What can be wrong with whit?