Search code examples
objective-ccore-datarelationshiprelationships

Core Data Relationships after entities are filled up with data


I have two entities Team & TeamMembers which are initially filled with data from two json files.

So there is no connection between those entities at the moment. Is it possible to make relationship at this point, after both entities are filled up with data?

Entities look like this now

Team

  • id
  • teamName

TeamMembers

  • id
  • idTeam
  • member

Solution

  • I you haven't done already, define relationships

    • members: to-many relationship from Team to TeamMembers,
    • team: to-one relationship from TeamMembers to Team,

    and set these up as mutual inverse relationships.

    Now, if you have a Team *theTeam and a TeamMember *theMember you just set

    theMember.team = theTeam
    

    to establish the relationship between these objects.

    If the number of teams and members is not too large, you could simply loop over all teams and members and set the relationship if the id matches.

    Alternatively, you can for each team execute a fetch request that fetches all members with the matching id, and then set the relationship.