旧RLMObject位于下方,primaryKey是AttributeId。我想将此密钥更改为@" Id"下一次建设。
UserItemObject.m
MC_choice
UserItemObject.h
@implementation UserItemObject {
}
+ ( NSString * )primaryKey; {
return @"AttributeId";
}
@end
然后我给AppDelegate写了一些代码;
@interface UserItemObject : RLMObject
@property(nonatomic, copy) NSString *Id;
@property(nonatomic, copy) NSString *AttributeId;
@end
RLM_ARRAY_TYPE(UserItemObject)
此代码给我一个错误;
[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";
}];
}
}];
我该如何解决这个问题?非常感谢。
答案 0 :(得分:1)
要更改主键属性,您需要更改+[UserItemObject primaryKey]
的返回值。
然后,要实际进行迁移,您将执行以下操作:
[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"];
}];
}
}];