NSFetchRequest con predicado que no devuelve objetos

In my Delegate I specify the following method for retrieving an NSSet of NSManagedObjects:

- (NSSet *) entitiesForName : (NSString *)entityName matchingAttributes : (NSDictionary *)attributes {
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext: [NSThread isMainThread] ? managedObjectContext : bgManagedObjectContext];

NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
[fetch setEntity: entity];

NSMutableArray *subPredicates = [[NSMutableArray alloc] init];
[attributes enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
    if ([value class] == [NSString class]) {
        NSString *sValue = (NSString *)value;
        [subPredicates addObject:[NSPredicate predicateWithFormat:@"%@ == '%@'", key, [sValue stringByReplacingOccurrencesOfString:@"'" withString:@"\\'"]]];
    } else {
        [subPredicates addObject:[NSPredicate predicateWithFormat:@"%@ == %@", key, value]];
    }
}];
NSPredicate *matchAttributes = [NSCompoundPredicate andPredicateWithSubpredicates:subPredicates];
NSLog(@"matchPredicate: %@", [matchAttributes description]);
[fetch setPredicate: matchAttributes];

NSError *error;
NSSet *entities = [NSSet setWithArray: [managedObjectContext executeFetchRequest:fetch error:&error]];

if (error != nil) {
    NSLog(@"Failed to get %@ objects: %@", entityName, [error localizedDescription]);
    return nil;
}

return [entities count] > 0 ? entities : nil;
}

I then initiate this method using an Entity I know exists and matching an attribute I know has some of the same value (I checked the sqlite file):

[self entitiesForName:@"Lecture" matchingAttributes:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:@"attending"]]

The console outputs the following (showing the predicate):

2013-09-11 22:47:20.098 CoreDataApp[1442:907] matchPredicate: "attending" == 0

Info on the NSObject Entity:
- property "attending" is a BOOL (translated to NSNumber in class)
- There are many records in this table (Lecture entity), half with "attending" value 0 and other half 1
- Using the method -entitiesForName above, it returns an empty set

Otra información:
I have another method defined to retrieve the same way, but without the predicate (retrieves all managedobjects) and this works going from the same table. I used this, and analysing the retrieved records from this also proves that some have "attending" 0 and some 1

Pregunta:
Is there something wrong with my -entitiesForName method that would cause the set to come back empty?

preguntado el 11 de septiembre de 13 a las 23:09

1 Respuestas

No deberías usar %@ for the key - you need to use %K for the key path.

Por ejemplo:

[NSPredicate predicateWithFormat:@"%K == %@", key, value]

Puede encontrar más información en el Guía de programación de predicados

In your current case the key is being treated as a string

Respondido el 11 de Septiembre de 13 a las 23:09

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.