Question

Three entities in CoreData:

  • User
  • EntityA
  • EntityB

Relationships:

  • EntityA has one-to-many Relationship with User & inverse
  • EntityA has one-to-one Relationship with EntityB & inverse
  • User & EntityB have no relationships

Objects created:

  • UserA creates an objectA of Type EntityA listing UserB and UserC as the relationship objects.
  • UserA also creates ObjectB of type EntityB, listing ObjectA, as its relationship Object

Accessing Objects

  • UserB logs in and fetches in EntityA and successfully downloads ObjectA.

Question: Will UserB be able access ObjectB? If so, can UserB access ObjectB with the following code: NSString *value = [ObjectA.OneToOneRelationshipBetweenEntityAandB valueForkey"@attributeFromObjectB"];

If not, how can UserB access ObjectB? What relationships do I need to establish?

I had asked a similar question earlier but I thought I gave too much information and made it confusing. I deleted that question and hopefully simplified it down to this one.

Thanks.

Was it helpful?

Solution

Assuming a unified core data model, you can easily access objects as long as there are relationships.

For the purpose of readability, I am redefining your variable / relationship names:

User <<-----> Group <<-----> Community

Community has many groups has many users. This is simple enough and looks like a viable setup.

To clarify: a user cannot create an object. Only a program can do that.

Group *newGroup = [NSEntityDescription 
       insertNewinsertNewObjectForEntityForName:@Group"
       inManagedObjectContext:self.managedObjectContext]; 

Community *newCommunity = [NSEntityDescription 
       insertNewinsertNewObjectForEntityForName:@Community"
       inManagedObjectContext:self.managedObjectContext]; 


userA.group = newGroup; 
userB.group = newGroup;
newGroup.community = newCommunity;

Now both userA and userB belong to newGroup and the group is one of the groups in newCommunity. userB is linked to the group, so it is really easy to access the community:

Community *aCommunity = userB.group.community;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top