How to fetch all items that does not have certain relationship?

Hello!

I’m trying to create a fetch request that returns all objects that doesn’t have a certain relationship.

Basically I’m developing a note-app that allows the user to organize the notes into folders. So I created a “to many” relationship between my two entities “Folder” and “Note”.

The user should start taking a note right away, without the need of setting a folder at the time of the creation of the note. All notes that does not have a folder specified by the user should be presented above the table view that shows all the notes with their folders names as section headers. So after finishing the note, the user should be able to organize the not-yet-organized notes.

Has anyone an idea how to create a fetch request, that return all objects that doesn’t have this kind of relationship!

Thank you!

The result of the fetch is a set, so you could achieve this easily with two queries - first, all entities with the relationship, then all entities. Then use subtracting(_:) to remove all relationship entities from the set of all entities.

1 Like