In my app I have two entities which are Items
and Lists
. Each item belongs to only one list and each list has many items. Thus in the modal the entities has the following relationships:
- For
Items
: to-one relationship (belongs_to_list
) pointing to one list - For
Lists
: to-many relationship (has_items
) pointing to many items
How can I fetch the items using a predicate to check whether the list it's related to is equal to a specific list I provide? I don't want to fetch the items through the list (like fetching objects of has_items
). I want to be able to use belongs_to_list
in a predicate to compare it to a managed object I have. I tried the following but it's not working. Any help please?
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Items" inManagedObjectContext:_managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"item_detail" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"list.list_name == %@", [self.currentList valueForKey:@"list_name"]];
[fetchRequest setPredicate:predicate];