如何使用 Realm.io (objective-c) 中的主键克隆对象

How to clone a object with primary key in Realm.io (objective-c)

我有一个对象模型,有一个主键,我想用另一个主键克隆那个对象,但是 realm.io 说 "this object already exist " 主键与第一个对象不同

  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];

有什么想法吗?

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