1

CoreData 中的三个实体:

  • User
  • EntityA
  • EntityB

关系:

  • EntityAUser与&具有一对多关系
  • EntityAEntityB与&具有一对一的关系
  • User&EntityB没有任何关系

创建的对象:

  • UserA创建一个objectATypeEntityA列表UserB和 UserC 作为关系对象。
  • UserA还创建ObjectB类型EntityB,列表ObjectA,作为它的关系对象

访问对象

  • UserB登录并获取EntityA并成功下载ObjectA

问:UserB可以访问ObjectB吗?如果是这样,可以UserB使用以下代码访问 ObjectB:NSString *value = [ObjectA.OneToOneRelationshipBetweenEntityAandB valueForkey"@attributeFromObjectB"];

如果没有,如何UserB访问ObjectB?我需要建立什么样的关系?

我之前曾问过类似的问题,但我认为我提供了太多信息并使其混乱。我删除了这个问题,并希望将其简化为这个问题。

谢谢。

4

1 回答 1

2

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;
于 2013-08-21T12:37:54.553 回答