I am trying to create a fetchRequest that shows me all values that are not already in another array. This code returns the array I expect:
NSArray *questionChoices = [self.currentQuestionChoiceGroup.questionChoices allObjects];
NSArray *filteredQuestionChoices = [questionChoices filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"NONE %@ == code", myarr]];
myarr contains 1 item, and that 1 item is excluded from the filtered results, as expected. However, this code doesn't work:
NSFetchRequest *request = [[NSFetchRequest alloc] init];
request.entity = [NSEntityDescription entityForName:@"QuestionChoice" inManagedObjectContext:self.managedObjectContext];
request.predicate = [NSPredicate predicateWithFormat:@"NONE %@ == code AND questionChoiceGroup == %@", myarr, self.currentQuestionChoiceGroup];
When I execute this fetch request, I get all the questionChoices that belong to the current questionChoiceGroup, including the one that has a code that's in myarr. It seems to completely ignore the first part of the AND statement.
I can't see any difference between the two that should lead to different results. Can anyone help?
EDIT Simpler explanation:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"questionChoiceGroup == %@ AND NONE %@ == code", self.currentQuestionChoiceGroup, [NSArray arrayWithObject:@"A"]];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"displayOrder" ascending:YES]];
request.entity = [NSEntityDescription entityForName:@"QuestionChoice" inManagedObjectContext:self.managedObjectContext];
request.predicate = predicate;
NSArray *filteredQuestionChoices1 = [self.managedObjectContext executeFetchRequest:request error:nil];
NSLog(@"my filtered question choices 1: %@", [filteredQuestionChoices1 valueForKey:@"code"]);
NSArray *filteredQuestionChoices2 = [filteredQuestionChoices1 filteredArrayUsingPredicate:predicate];
NSLog(@"my filtered question choices 2: %@", [filteredQuestionChoices2 valueForKey:@"code"]);
filteredQuestionChoices1 contains an item with a code of "A". filteredQuestionChoices2 does not have that item. How can the second predicate filter out anything that the first predicate didn't filter out? Both statements use the exact same predicate. Why am I getting objects back from the fetchedRequest if the exact same predicate will filter those objects out if I use the predicate on the array?