rattletrap99 rattletrap99 - 5 months ago 17
iOS Question

Predicate quandary

I want to modify some Core Data entities via a

for in
loop. The fetched objects should be filtered via a predicate. But the loop never fires because no objects are fetched.

I suspect there's an issue with the predicate, although I've vetted it pretty closely. Here's the code:

NSString *replenishString = @"Replenishment";
NSDate *now = [NSDate date];

NSPredicate *transFRCPredicate = [NSPredicate predicateWithFormat:@"(account == %@) AND ((transDate >= %@) AND (transDate <= %@)) AND (category != %@)",self.detailTransaction.account,self.detailTransaction.transDate,now,replenishString];


NSFetchedResultsController *transactionsToAdjustFRC = [WMMGTransaction MR_fetchAllGroupedBy:@"account" withPredicate:transFRCPredicate sortedBy:@"transDate" ascending:NO];

(...)

for (WMMGTransaction *transaction in transactionsToAdjustFRC.fetchedObjects)
{
NSLog(@"Inside for loop--delta = %@",delta);
transaction.category = @"Changed +";

transaction.pointBalance = [transaction.pointBalance decimalNumberByAdding: delta];
NSLog(@"Inside for loop--pointBalance = %@",transaction.pointBalance);
}


Any ideas or guidance?

Thanx!

Answer

Take care when using "!=" in predicates: a nil value will not match. Include a specific test for nil:

NSPredicate *transFRCPredicate = [NSPredicate predicateWithFormat:@"(account == %@) AND ((transDate >= %@) AND (transDate <= %@)) AND (category != %@ OR category == nil)",self.detailTransaction.account,self.detailTransaction.transDate,now,replenishString];
Comments