如何在Realm.io中克隆具有主键的对象(objective-c)

时间:2015-05-20 17:40:03

标签: ios objective-c realm

我有一个对象模型,有一个主键,我想用另一个主键来修复该对象,但是realm.io说"这个对象已经存在" 主键与第一个对象

不同
  RLMRealm *realm = [RLMRealm defaultRealm];
  [realm beginWriteTransaction];

  ActivityTreeModel *originalActivity = [ActivityTreeModel objectForPrimaryKey:activityUuid];
  ActivityTreeModel *cloneActivity = originalActivity;
  NSString *primaryKey = [NSString stringWithFormat:@"%@+%@",[originalActivity uuid], executionUuid ];

  @try {
       cloneActivity.uuid = primaryKey;
       cloneActivity.execution_uuid = executionUuid;
  }
  @catch (NSException *exception) {
        NSLog(@"error de realm %@",exception);
  }
  @finally {

  }

  [ActivityTreeModel createOrUpdateInRealm:realm withValue:cloneActivity];
  [realm commitWriteTransaction];

任何想法?

1 个答案:

答案 0 :(得分:2)

创建一个新的ActivityTreeModel对象:[[ActivityTreeModel alloc] init],然后将其所有属性设置为您要复制的属性,但使用不同的主键。