0

Old RLMObject is there below and primaryKey is AttributeId. I want to change this key to @"Id" next build.

UserItemObject.m

@implementation UserItemObject {

}

+ ( NSString * )primaryKey; {
    return @"AttributeId";
}

@end

UserItemObject.h

@interface UserItemObject : RLMObject
   @property(nonatomic, copy) NSString *Id;
   @property(nonatomic, copy) NSString *AttributeId;
@end
RLM_ARRAY_TYPE(UserItemObject)

And then I wrote some code to AppDelegate;

  [RLMRealm setSchemaVersion:1 forRealmAtPath:[RLMRealm defaultRealmPath] withMigrationBlock:^(RLMMigration *migration, NSUInteger oldSchemaVersion) {
      if ( oldSchemaVersion < 1 ) {
          [migration enumerateObjects: UserItemObject.className block:^(RLMObject *oldObject, RLMObject *newObject) {
                newObject[ @"primaryKeyProperty" ] = @"Id";
          }];
      }
  }];

This code give me an error ;

*** Terminating app due to uncaught exception 'RLMException', reason: 'Invalid property name'

How can I solve this issue? Thanks a lot.

serdaryillar
  • 413
  • 5
  • 16

1 Answers1

1

To change the primary key property, you'll need to change the return value of +[UserItemObject primaryKey].

Then, to actually do the migration, you'll do:

[RLMRealm setSchemaVersion:1 forRealmAtPath:[RLMRealm defaultRealmPath] withMigrationBlock:^(RLMMigration *migration, NSUInteger oldSchemaVersion) {
      if ( oldSchemaVersion < 1 ) {
          [migration enumerateObjects: UserItemObject.className block:^(RLMObject *oldObject, RLMObject *newObject) {
                newObject[ @"Id" ] = oldObject[@"AttributeId"];
          }];
      }
  }];
Daniel Hepper
  • 28,981
  • 10
  • 72
  • 75
segiddins
  • 4,110
  • 1
  • 17
  • 22