【问题标题】:Adding Data in Entities with the Relationship在具有关系的实体中添加数据
【发布时间】:2014-05-13 05:37:28
【问题描述】:

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

现在

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

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

我已经像这样声明了这两个实体。

问题:-

  1. 由于我已经声明了 Room 和 Location 的对象,首先我需要填写 位置的数据,然后我需要填写房间的数据和 那么我需要在location方法中添加房间吗?对吗?

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

【问题讨论】:

    标签: ios database core-data entity-relationship iphonecoredatarecipes


    【解决方案1】:

    回答

    1. 这里的顺序并不重要,而是引用其他实体对象。您可以使用代码

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

      [locObj addWithRoomObject:roomObj];
      

    更新

    添加更多房间:

    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..
    

    【讨论】:

    • 如何在此处添加一个->多个连接?如何在同一位置添加两个以上的房间?
    猜你喜欢
    • 2018-06-17
    • 1970-01-01
    • 1970-01-01
    • 2011-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-19
    • 2014-05-13
    相关资源
    最近更新 更多