Reputation: 409
I had a conceptual problem with Core Data.
I have an entity called OBJ which has an OBJ entity in a relationship. I get very eloquent messages like "An error occured."
when I test my model.
For each OBJ entity, I wish to define one or more ACTIONs associated with another OBJ entity. So I guess my mistake comes from a loop, but not sure.
At first, I defined an inverse relationship (as advised by the doc, a relationship should always have its inverse):
ENTITY OBJ
Attributes:
name
status
Relationships:
actions
<-------- >>
ENTITY ACTION
Attributes:
name
Relationships:
obj
Well, it does not work, because the OBJ always refers to itself. I have therefore decoupled relationships. The ACTION points to an OBJ and the OBJ points to several ACTIONs, without reciprocity. Error message.
My basic idea is that each of the ACTIONs associated with a particular OBJ can run if the status of another OBJ allows it.
How to build this to be MVC consistent? The basic idea seems simple, it is his achievement that is less so. Is it a wrong Core Data modeling? Do I make a mistake at the controller level? Interface? Note that at this level I have not written a single line of code.
Thank you!
Upvotes: 0
Views: 423
Reputation: 409
I found a solution by adding a new relationship between "OBJ" and "ACTION".
OBJ ACTION
… …
Relationships Relationships
targetObject <------->> actions
conditionObject <------->> condition
If I don't, the inverse relationship always refers to the same (self) object. So, by changing a relationship, I was changing the object itself!
By adding a new one-to-many relationship, I can indirectly make a OBJ to OBJ relation (the targetObject and conditionObject may be the same one) AND keep the graph consistent.
Each day, I understand better what "Core Data is not for beginners" means…
Upvotes: 0
Reputation: 80273
If you want to
define one or more ACTIONS associated with another OBJ
it means that one OBJ can have many ACTIONS and one ACTION can have many OBJs.
Therefore you should define a relationship between ACTION and OBJ that is to-many in both directions.
OBJ.actions <<-------->> ACTION.obj
Unless you intend to have "sub-objects" (such as CATEGORY and subCATEGORY) you should not have a relationship between OBJ and OBJ.
Upvotes: 1