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!