Three entities in CoreData:
User
EntityA
EntityB
Relationships:
EntityA
has one-to-many Relationship with User
& inverseEntityA
has one-to-one Relationship with EntityB
& inverseUser
& EntityB
have no relationshipsObjects 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 ObjectAccessing 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.
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;