0

enter image description here实体中添加数据与关系

在我的核心数据模型,我有这样的关系(见图)

现在

Location *locObj = [NSEntityDescription insertNewObjectForEntityForName:@"Location" 
                   inManagedObjectContext [self managedObjectContext]]; 

Room *roomObj = [NSEntityDescription insertNewObjectForEntityForName:@"Room"  
             inManagedObjectContext:[self managedObjectContext]]; 

我声明这两个实体像这样。

问题: -

  1. 正如我已经宣布间和地点的对象,首先我需要填充的位置 数据,然后我需要填写房间的数据和 然后我需要在位置方法中添加空间?是否正确?

  2. 如果我有大数据?每次我需要添加对象还是有任何我们可以连接的自定义支持的类?

回答

1

回答

  1. 的顺序并不重要,但参考其他实体对象。您可以使用代码

    [locObj addWithRoomObject:roomObj]; 
    roomObj.withLocation = locObj; 
    
  2. 没有这样的自定义支持的类。您可以在核心数据中使用自动生成的访问器,并自动处理。您可以调用函数:

    [locObj addWithRoomObject:roomObj]; 
    

UPDATE

要添加更多的房间:

Location *locObj = [NSEntityDescription insertNewObjectForEntityForName:@"Location" 
                  inManagedObjectContext [self managedObjectContext]]; 

locObj.locationName = LOCATION_NAME; 

Room *room1 = [NSEntityDescription insertNewObjectForEntityForName:@"Room"  
            inManagedObjectContext:[self managedObjectContext]]; 
// Fill room1 details 
... 
room1.withLocation = locObj; 
[locObj addWithRoomObject:room1]; 

Room *room2 = [NSEntityDescription insertNewObjectForEntityForName:@"Room"  
            inManagedObjectContext:[self managedObjectContext]]; 
// Fill room2 details 
... 
room2.withLocation = locObj; 
[locObj addWithRoomObject:room2]; 

// so on.. 
+0

如何添加单>这里的很多连接?如何在同一地点添加两个以上的房间? –

+0

@ Jean-LucGodard我更新了答案 –