ios - Realm primary key migration - objective C -


old rlmobject there below , primarykey attributeid. want change key @"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 wrote code 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 error ;

*** terminating app due uncaught exception 'rlmexception', reason: 'invalid property name' 

how can solve issue? lot.

to change primary key property, you'll need change return value of +[useritemobject primarykey].

then, 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"];           }];       }   }]; 

Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -