Can two CoreData objects have more than one "many" relationship between them?

My model has a Filter object and a Color object. I want two To-Many relationships between them: backgrounds and foregrounds. (that is, each “Filter” contains an array of background colors and an array of foreground colors, all of type “Color”).

I have two problems. Firstly, when I establish relationships, the inversion of “Color” can have only one value (I assume that this means that I cannot do what I want). Secondly, I cannot figure out how to create a predicate to extract only backgrounds or only foreground. I assume that they will all be combined together (if they work at all):

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Color"]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"filter == %@", self]; 

I am going to solve this problem by converting Color to two Foreground and Background objects, but I do not want to do this without understanding why one object will not work. Did I miss something?

NOTE. This is apparently not intuitive for me. I ran into this exact problem again, but this time I did not use predicates, so I did not immediately find the problem. I had a very similar setting, but when I tried to remove the child, it will disappear first, but when I restarted the application, it will appear again. The more children I removed, the more I did. In the end, I noticed that the same children were in both arrays of relationships, and when I looked at the graphic version of the model, only one of these relationships had an arrow associated with the child. Finally, I remembered that I had this problem before! I do not understand very well what is happening, but it is clear that there are two child "arrays" where they merge together in a strange way.

+4
source share
1 answer

Yes.

Theory of Multiple Relationships

Say you have an entity that is a boy or girl. Each child can have a set (for many) of all his brothers and sisters. But each child can have two more sets (for many) of all 1 of them: brothers and 2 sisters.

The inverse of “siblings” would be “isSiblingTo” (or siblings). And the flip side of the “brothers” and “sisters” will be “isBrotherTo” and “isSisterTo”.

It was very simple because both objects where they are the same.

Say, instead, I am a child who has a set of crayons and a set of toys. I probably have my red pencil in both of these sets, i.e. I have a lot of relationships with him.

It all comes down to the fact that the red pencil is both my CRAYON and my TOY. As in the example above, the brothers are their brothers and sisters, and their sisters are their brothers and sisters.

Update:

You do not need to be part of both relationships. You can have a child without a relationship of brothers and sisters, but with a relationship of brothers and sisters, and other children will have a reverse relationship with them (this is closer to your example with a color + filter)

Applied in your example

So, if you have a use for this, this is doable. Just think about your relationships in everyday language and set the opposite as isForegroundColorTo and isBackgroundColorTo.

multi-relationship data model data model with multiple to-many relations

color model definition color model definition

filter model definition filter model definition

+4
source

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


All Articles