Post

Replies

Boosts

Views

Activity

Reply to NSPredicate filtering using set operations
I'm using Core Data entities with a many-to-many relationship (GroceryList <<->> GroceryItem). I cannot change the Core Data model without impacting other functionality. Entity: GroceryList Attribute: groceryListName: String? Relationship: GroceryItems Entity: GroceryItem Attribute: groceryItemName: String? Relationship: GroceryLists I need a view with filter options to display Grocery Lists. The filter options populates a Set<String> called desiredGroceries from toggles. In this example, the user would want to see Grocery lists that had Milk and Eggs, so would toggle Milk, toggle Eggs, and not toggle any others. This results in: desiredGroceries("Milk", "Eggs") To get the lists, I use: fetchRequest = FetchRequest<GroceryList>(entity: GroceryList.entity(), sortDescriptors: [], predicate: NSPredicate(format: "SUBQUERY(groceryItems, $x, $x.name IN %@).@count>0", desiredGroceries)) My problem is that if the user selects more than one of the filter options (ie Milk and Eggs), it will show lists that have milk as well as lists that have eggs. This code works as expected but not as desired. I just want the lists that have milk AND eggs, not milk OR eggs.
Oct ’20