Spaceghost
Spaceghost

Reputation: 45

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

i have an object model, with a primary key and i want to do a clone of that object with another primary key but realm.io says "this object already exist " the primary key is not the same as the first object

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

any ideas?

Upvotes: 1

Views: 738

Answers (1)

pteofil
pteofil

Reputation: 4163

Create a new ActivityTreeModel object: [[ActivityTreeModel alloc] init], and then set all it's properties as to the one you want to copy, but with the different primary key.

Upvotes: 2

Related Questions