How do I implement cross-model fetched properties in a core data model? Simply put, let’s say I have two models — a CompanyStructure model that describes departmental hierarchy, and a Employee model that describes all employees. How would I set an employee’s department as a fetched property?
I found a similar question asked here. It’s a good description of the problem I’m facing:
When I setup a fetched property for state in my document entity (in
Xcode modeller) it needs the destination set – however, the
destination popup only shows entities in the current model. What I
want (I think) is a predicate something like:
State.id == $FETCH_SOURCE.state_idin order to relate the document entity to the globally defined state
entity in the other model.
BTW, I don’t know about $FETCH_SOURCE or $FETCH_ANYTHING_ELSE, so I don’t really understand the code block in the quoted segment above. I do know that I can’t set the entity for an employee’s department fetched property because they’re in separate models.
EDIT — Merging the two data models into one is not an option.
Fetched properties are part of the model, so by definition they are not cross-model. If you want to refer to more than one model you have to do it in code. Also, the definition of the connection between the Employees and Departments can only be done within a model, unless you use your own UIDs and thus bypass the model metaphor.
They way I do similar tasks, I add additional .h and .m files to my classes, like
ClassName+Additions.hand I declare “properties” as methods that can be called just like properties such as[smith department]. In these files, I can#includeall the necessary classes from the other model and maintain separate managed object contexts.