0
votes

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:

  1. For Items: to-one relationship (belongs_to_list) pointing to one list
  2. 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];
1

1 Answers

1
votes

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.