Swift : core Data join entity

I Know that Nsfetchedcontroller does not support multiple entity. But i would like to know what is the efficient way to solve the pb below

Lets say i have two entity : catalogue and selectedProduct
I am fetching all the entity catalogue to the user so he can select some products the task is: only propose the products in the catalogue that are not in selectedproduct

In sql it will be like select * from catalogue where productName not in (select productName from selectedProduct)

Can anybody help me plz.

enter image description here

1 answer

  • answered 2018-02-18 10:29 pbasdf

    In think the following predicate should filter the products as you require:

    fetch.predicate = NSPredicate(format:"SUBQUERY(lescourses, $lc, $lc.listedescourses == %@).@count == 0",listdescourses)

    I've used SUBQUERY. In theory it could be done with NONE or NOT ANY, but I sometimes find CD doesn't parse them correctly. Note that, as per comments, the FRC will observe only the changes to TblProduits, to if you were to modify an existing TblLesCourses object to establish a relationship to listdescourses, the FRC will not (unless you redo the performFetch) recognise that the related TblProduits objects should no longer be included.