问题
I am migrating a data model I have in Realm to Core Data (so I can sync with Ensembles). The model is a graph, and in Realm I am managing the integrity of the graph myself with rules about deletions etc in transactions.
I have made an example graph here that aims to describe the crux of my problem.
All objects in my graph are Things. Things have an ordered to-many relationship with their subThings. However, subThings have multiple ways they link back. For example, one DerivedFromThing type has two to-many relationships (their inverses would be in the subThings of another Thing).
So, my graph has a full set of inverse relationships, but not of a fashion that appears to be modelable in Core Data.
Since I already have the code to manage integrity, I am happy to look after all the relationships myself. However, I am getting warnings in my build, so I wanted to double-check I am taking the right approach.
Is there a way to model my many-many relationships in Core Data? If not, are there issues I am going to face ignoring my warnings.
Thank you.
回答1:
I would recommend avoiding unidirectional relationships in CoreData: in my experience when you say nothing about the inverse, CD implicitly treats it as "to-one", which is probably not what you want.
Whilst CD can handle many-many relationships, in your case I think you might need to implement your relationships by directly modelling an intermediate entity (or possibly two or more). For example, you might have an entity named Association
, with
- an attribute of
associationType
("parent", "super" or whatever) - a to-one relationship
associatedTo
to theThing
entity (with a to-many inverse) - a to-one relationship
associatedFrom
to theDerivedFromThing
entity (also with a to-many inverse)
It's difficult to advise further, since I suspect your true model is more complicated than your example, but the above approach should provide a good degree of flexibility. See "Modeling a Relationship Based on Its Semantics" in the Core Data Programming Guide for more information.
来源:https://stackoverflow.com/questions/55121385/maintaining-complex-unidirectional-relationships-in-core-data