What is wrong with this code?
I know this is a silly predicate. But it’s just to show that if this was working, it should be filter everything out, right?
Somehow, it’s not. I’m getting every Month object in my DB, when I should be getting none.
I guess this tells me that the problem might not be in the predicate?
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Month" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setPredicate: [NSPredicate predicateWithFormat:@"1 == 0"]];
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"month_" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
[fetchRequest setFetchBatchSize:20];
NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext
sectionNameKeyPath:@"month_"
cacheName:@"Root"];
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[_fetchedResultsController sections] count];
}
What I actually wanted to do is,
[NSPredicate predicateWithFormat:@"year.year_ == %d", year]
And I also tried this,
[NSPredicate predicateWithFormat:@"month_ == %d", 1]
Just to see if it works. But nothing.
edit:
At this point, no matter what predicate I set, as long it’s a valid, well formatted predicate. It doesn’t complaint, and get’s me all objects. It’s just not filtering anything.
I’m pretty much using boiler plate code from Apple’s documentation.
Can anyone help me figure this out?
Thank you!
If using a cache, then the fetchRequest’s cache should be deleted, before being reused.
Calling deleteCacheWithName: fixed the problem.