NSPredicate does not return results with a query for sampling, works with filtering arrays

My situation is simple: I have some entries in my main data store. One of their attributes is a string called "localId". Where I would like to find an entry with a specific localId value. The obvious way to do this is NSFetchRequest and NSPredicate. However, when I install this, the query returns null entries.

If, however, I use a fetch query without a predicate, returning all the records, and just iterate over them, looking for the target value of localId, I find the record I'm looking for. In other words, there is a record, but the query query cannot find it.

My other methods, in which I use select queries and predicates, work as expected. I do not know why this failure.

I want to do this:

- (void)deleteResultWithLocalID:(NSString *)localId { NSFetchRequest *request = [[NSFetchRequest alloc] init]; [request setEntity:[NSEntityDescription entityForName:@"WCAAssessmentResult" inManagedObjectContext:context]]; [request setPredicate:[NSPredicate predicateWithFormat:@"localId == %@", localId]]; NSError *error = nil; NSArray *results = [context executeFetchRequest:request error:&error]; NSAssert(error == nil, ([NSString stringWithFormat:@"Error: %@", error])); if ([results count]) [context deleteObject:[results objectAtIndex:0]]; else NSLog(@"could not find record with localID %@", localId); [self saveContext]; } 

But I ultimately have to do this:

 - (void)deleteResultWithLocalID:(NSString *)localId { NSFetchRequest *request = [[NSFetchRequest alloc] init]; [request setEntity:[NSEntityDescription entityForName:@"WCAAssessmentResult" inManagedObjectContext:context]]; NSError *error = nil; NSArray *results = [context executeFetchRequest:request error:&error]; NSAssert(error == nil, ([NSString stringWithFormat:@"Error: %@", error])); for (WCAAssessmentResult *result in results) { if ([result.localId isEqualToString:localId]) { NSLog(@"result found, deleted"); [context deleteObject:result]; } } [self saveContext]; } 

Any clues as to what could go wrong?

change

I found that I can use the predicate that I create to get the expected results after I execute the select query. So the following also works:

 - (WCAChecklistItem *)checklistItemWithId:(NSNumber *)itemId { NSFetchRequest *request = [[NSFetchRequest alloc] init]; [request setEntity:[NSEntityDescription entityForName:@"WCAChecklistItem" inManagedObjectContext:context]]; NSArray *foundRecords = [context executeFetchRequest:request error:nil]; foundRecords = [foundRecords filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"serverId == %@", itemId]]; if ([foundRecords count]) { return [foundRecords objectAtIndex:0]; } else { NSLog(@"can't find checklist item with id %@", itemId); return nil; } } 

UPDATE

I came across someone else having this problem:

http://markmail.org/message/7zbcxlaqcgtttqo4

He also did not find a solution.

Blimey! I'm at a dead end.

Thanks!

+8
ios core-data nsfetchrequest nspredicate
source share
2 answers

I hate to talk about it, but the most common cause of these problems is a simple typo. Make sure your attribute names and predicate match. Make sure your property names and attribute names match. If the property reference works, but the attribute name is missing, there may be a mismatch.

You can check the latter by comparing the return:

 [result valueForKey:@"localID"] 

... with a return:

 result.localID 
+1
source share

If localId is a numeric value, you should use the NSNumber object in predicate formation instead of NSString .

 [request setPredicate:[NSPredicate predicateWithFormat:@"localId == %@", [NSNumber numberWithString:localId]]]; 
Format strings

NSPredicate automatically assigned to NSString objects.

+4
source share

All Articles