Retrieving master data objects but not sub-objects

Say I have entities organized in a hierarchy with Parent being the root entity, and Child is a subclass of Parent . I would like to configure NSArrayController to retrieve only Parent objects, but not Child .

If you set the Entity Name of the array controller in Interface Builder to Parent , it will select all the Parent and Child objects. I originally tried to set the fetch predicate for an array controller in Interface Builder for:

 entity.name == "Parent" 

This worked for the XML repository, but when I switched to the SQLite store, it no longer worked. I get the following error:

 keypath entity.name not found in entity <NSSQLEntity xxx> 

As a job, I set up a filter predicate (with the same entity.name predicate as above) in my awakeFromNib to filter only parent objects. Apparently, this predicate is valid after objects are in memory, but you cannot use it in SQL-backed fetch predicate.

Is there a way to get only Parent objects, but not Child entities using a select predicate that works with SQLite repository? It seems wasteful to pull objects that you just ignore with the filter predicate.

+4
source share
2 answers

I tried using includesSubentities , but in the end it did not work completely. It turns out that changes to the essence lead to the fact that the contents of the array controller are updated without fetching if you "automatically prepare the content" set to "Yes", thereby bypassing the user precedent of fetching. In the opposite direction, setContent: displayed, called in response to MOC notifications.

The only way I found this is to use a filter predicate.

+3
source

Leopard introduced the includesSubentities property for NSFetchRequest specifically for this purpose. You need to subclass NSObjectController or NSArrayController in order to provide a selection request that will be used through its defaultFetchRequest property, or to change the selection request that it uses by overriding its -fetchWithRequest:... method.

+7
source

Source: https://habr.com/ru/post/892294/


All Articles