Creating a list of parent entities using a query / predicate for fetching master data

I have a basic data model that uses a database repository that contains objects with classic parent / child relationships. Each parent has several children, each of which has one parent, creating a multi-level hierarchy.

What I would like to do is use the predicate in the select query to return a list of all entities between this object and the root.

If I did this in code, I would work on the “parents” chain until I hit the root object, but I would like to do this in the predicate so that the search remains “in the database”, This will be part of the search a, therefore there should be relatively fast.

Is it possible?

+4
source share
2 answers

Predicates must be bound to cross an unknown number of objects in the relationship graph. You must specifically say which entities and relationships must go through. You cannot say, "Move the relationship so-and-so until you hit zero."

Premature optimization is the root of all evil. I would not worry about the speed of passing relationships in living objects. Core Data is much faster than you might expect in such things. To optimize in this case, set to extract as errors, so no data is loaded only by relationships, and I don’t think you will have performance problems.

As an aside, look at your nomenclature, entities must be managed by objects, since classes refer to instances. Abstract objects, managed concrete objects. Objects are attributes of a data model only. For example, in this case, you can have only one self-regulatory object in the data model, which can be used to create a graph of millions of managed objects.

You can find the parent objects in the same way you find superclasses, but this is actually not what you asked.

+2
source

I don’t think it’s possible to do this in one round trip using the parent / child relationship. However, there are other methods. If you are concerned about performance (and measured it to make sure that it is actually a problem for your average data set), you may find this useful: Storing hierarchical data in a database

0
source

All Articles