NSPredicate filtering using set operations

I'm trying to filter such that criteria is met for each AND every item in a set vice each OR every item.

If I use
Code Block
groceryItem IN %@, desiredGroceries
it acts like an OR statement.

Example: I'm trying to view all grocery lists that included milk AND eggs (defined in desiredGroceries Set).

If I use the above code, it returns all lists that included milk as well as lists that included eggs.
But I only want the lists that include both milk and eggs.

What's the correct Predicate for this?
Could you show more code, and the complete predicate declaration ? That will make it easier to answer.
It's a part of a larger subquery since I'm going across relationships, but here's the relevant part...

Code Block
SUBQUERY(groceryItems, $x, $x.name IN %@).@count>0, desiredGroceries)


where
Code Block
desiredGroceries = Set<String>{"Milk", "Eggs}
and add or remove items via buttons.

It is normal it acts as an OR.
You ask for name to be in the set : milk of eggs do so.

To get an AND, need to change the predicate.

create an array
Code Block
let desired = ARRAY(desideredGroceries) // Should be at least 2 items


and query like (did not test)
Code Block
SUBQUERY(groceryItems, $x, ($x.name.contains(%@.first!) && $x.name.contains(%@.last!)).@count>0, desiredGroceries)

You can also use contains(where:)

It is not clear however how groceryItem is defined: does name contain both eggs and milk for instance ?

I do not fully understand the syntax here

Code Block
SUBQUERY(groceryItems, $x, $x.name IN %@).@count>0, desiredGroceries)

There seems to be missing a parenthesis.

Could you detail how you build the predicate:

let predicate = NSPredicate(format: <#T##String#>, <#T##args: CVarArg...##CVarArg#>)
and how you use it in subquery

Could try to get inspired from
https://stackoverflow.com/questions/13242383/what-is-wrong-in-my-nested-subquery-predicate

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:
Code Block
desiredGroceries("Milk", "Eggs")


To get the lists, I use:
Code Block
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.
NSPredicate filtering using set operations
 
 
Q