Reputation: 1341
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:
Items
: to-one relationship (belongs_to_list
) pointing to one listLists
: to-many relationship (has_items
) pointing to many itemsHow 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];
Upvotes: 0
Views: 153
Reputation: 33428
If I understood well your question, the following predicates would be correct:
[NSPredicate predicateWithFormat:@"belongs_to_list == %@", [self currentList]];
Let me know if this does work for you.
Upvotes: 1