Piero
Piero

Reputation: 9273

Use NSPredicate to retrieve information in core data from relationship

Hello i have this Core data:

enter image description here

then i do this:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription 
                           entityForName:@"MyDate"  inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];

now i want use the NSPredicate to take only the MyDate with the Task for the taskes relationship that have isView = NO and the taskDate <= today, and that the count of all Task for each MyDate is > 0...there is a way?

Upvotes: 3

Views: 531

Answers (2)

brainfree
brainfree

Reputation: 827

I don't have enough points to comment, so jumping off of Nikita's answer, all you need to do is the following:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY taskes.isView = NO AND ANY taskes.taskDate <= %@", todayDate];

One way you can get today's date (there might be better ways):

NSDate *date = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/yyyy"];
NSString *dateString = [dateFormat stringFromDate:date];
NSDate *todayDate = [dateFormat dateFromString:dateString];

Upvotes: 2

Nikita Pestrov
Nikita Pestrov

Reputation: 5966

Use [NSPredicate predicateWithFormat:@"taskes.@count>0 AND (ANY (taskes.isView = NO AND taskes.taskDate<%@)",today]; " Not sure,but maybe it would work

Upvotes: 0

Related Questions