Karamjeet Singh
Karamjeet Singh

Reputation: 490

CoreData NSPredicate fetch data by Date

Please help me to make this predicate

[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"subsidiary_of_cat_id == %@ AND (valid_from < %@) AND (valid_till > %@ OR valid_till == %@) ",catId,[NSDate date],[NSDate date], dateTo,]]

receive crash error: Unable to parse the format string

Conditions to fetch data from CoreData

Condition 1: now() < valid_till OR now() = [NSDate date]
Condition 2: now() > valid_from

Thanks!

RESOLVED: Updated code

NSMutableArray *predicatesArray = [NSMutableArray array];
    [predicatesArray addObject:[NSPredicate predicateWithFormat:@"%@ == subsidiary_of_cat_id",catId]];
    [predicatesArray addObject:[NSPredicate predicateWithFormat:@"%@ > valid_from",[NSDate date]]];
    [predicatesArray addObject:[NSPredicate predicateWithFormat:@"%@ < valid_till",[NSDate date]]];
    [predicatesArray addObject:[NSPredicate predicateWithFormat:@"%@ == valid_till”,dateTo];
[NSCompoundPredicate orPredicateWithSubpredicates:predicatesArray]

Upvotes: 1

Views: 325

Answers (1)

pbasdf
pbasdf

Reputation: 21536

You don't need to wrap the string in stringWithFormat:

[NSPredicate predicateWithFormat:@"subsidiary_of_cat_id == %@ AND (valid_from < %@) AND (valid_till > %@ OR valid_till == %@) ",catId,[NSDate date],[NSDate date], dateTo];

Upvotes: 2

Related Questions