RLMArray 到 RLMLinkingObjects 迁移
RLMArray to RLMLinkingObjects migration
我有一个包含两个对象的模型:SFCHChat
和 SFCHMessages
,它们具有一对多关系。
由于从 Realm 0.98 升级到 2.0,我现在在生产中遇到以下异常:
Migration is required due to the following errors: - Property 'SFCHChat.chatId' has been made optional. - Property 'SFCHChat.lastReadedMessageId' has been made optional. - Property 'SFCHMessage.chat' has been added. - Property 'SFCHMessage.messageId' has been made optional. - Property 'SFCHMessage.chatId' has been made optional. - Property 'SFCHMessage.userId' has been made optional. - Property 'SFCHMessage.message' has been made optional. - Property 'SFCHUser.initials' has been made optional. - Property 'SFCHUser.userId' has been made optional. - Property 'SFCHChat.messages' has been removed.
因此,我向 + (NSArray *)requiredProperties
添加了可选项,但不清楚如何处理 SFCHChat.messages
和 SFCHMessage.chat
。这是这些对象的代码差异:
### SFCHMessage.h
-@property (readonly) SFCHChat *chat;
+@property SFCHChat *chat;
### SFCHMessage.m
-- (SFCHChat *)chat
-{
- return [[self linkingObjectsOfClass:@"SFCHChat" forProperty:@"messages"] firstObject];
-}
-
### SFCHChat.h
-@property RLMArray<SFCHMessage> *messages;
+@property (readonly) RLMLinkingObjects *messages;
### SFCHChat.m
++ (NSDictionary *)linkingObjectsProperties {
+ return @{
+ @"messages": [RLMPropertyDescriptor descriptorWithClass:SFCHMessage.class propertyName:@"chat"],
+ };
+}
+
总而言之,我将 Realm 对象上的数组迁移到 RLMLinkingObjects
属性,因为 linkingObjectsOfClass
已被弃用。因此,我没有将 SFCHMessage
个对象添加到 SFCHChat.messages
RLMArray,而是向每个 SFCHMessage
添加了一个 chat
属性,并用 RLMLinkingObjects
.
看起来更改为 (readonly)
被视为删除领域中的属性,而删除它被视为添加属性。我的案例真的需要迁移块吗?我将如何执行此迁移?
是的,我认为您可以修改 Realm 的架构并 运行 迁移。
当 Realm 将可选值添加到所有属性时,您可以通过简单地显式将其明确设为必需来缓解这些问题,但在添加或删除属性时,只需要迁移即可。
除非属性中的数据会在迁移中丢失,除非您选择保留它们,运行进行 Realm 迁移就像简单地添加一个空块一样简单。
RLMRealmConfiguration *configuration = [RLMRealmConfiguration defaultConfiguration];
configuration.schemaVersion = 1; //Must be incremented by 1 each time the schema is changed.
configuration.migrationBlock = ^(RLMMigration *migration, uint64_t oldSchemaVersion) { };
[RLMRealmConfiguration setDefaultConfiguration:configuration];
这将使您下次调用 [RLMRealm defaultRealm]
时,迁移将 运行 并且您的文件将被升级。
我有一个包含两个对象的模型:SFCHChat
和 SFCHMessages
,它们具有一对多关系。
由于从 Realm 0.98 升级到 2.0,我现在在生产中遇到以下异常:
Migration is required due to the following errors: - Property 'SFCHChat.chatId' has been made optional. - Property 'SFCHChat.lastReadedMessageId' has been made optional. - Property 'SFCHMessage.chat' has been added. - Property 'SFCHMessage.messageId' has been made optional. - Property 'SFCHMessage.chatId' has been made optional. - Property 'SFCHMessage.userId' has been made optional. - Property 'SFCHMessage.message' has been made optional. - Property 'SFCHUser.initials' has been made optional. - Property 'SFCHUser.userId' has been made optional. - Property 'SFCHChat.messages' has been removed.
因此,我向 + (NSArray *)requiredProperties
添加了可选项,但不清楚如何处理 SFCHChat.messages
和 SFCHMessage.chat
。这是这些对象的代码差异:
### SFCHMessage.h
-@property (readonly) SFCHChat *chat;
+@property SFCHChat *chat;
### SFCHMessage.m
-- (SFCHChat *)chat
-{
- return [[self linkingObjectsOfClass:@"SFCHChat" forProperty:@"messages"] firstObject];
-}
-
### SFCHChat.h
-@property RLMArray<SFCHMessage> *messages;
+@property (readonly) RLMLinkingObjects *messages;
### SFCHChat.m
++ (NSDictionary *)linkingObjectsProperties {
+ return @{
+ @"messages": [RLMPropertyDescriptor descriptorWithClass:SFCHMessage.class propertyName:@"chat"],
+ };
+}
+
总而言之,我将 Realm 对象上的数组迁移到 RLMLinkingObjects
属性,因为 linkingObjectsOfClass
已被弃用。因此,我没有将 SFCHMessage
个对象添加到 SFCHChat.messages
RLMArray,而是向每个 SFCHMessage
添加了一个 chat
属性,并用 RLMLinkingObjects
.
看起来更改为 (readonly)
被视为删除领域中的属性,而删除它被视为添加属性。我的案例真的需要迁移块吗?我将如何执行此迁移?
是的,我认为您可以修改 Realm 的架构并 运行 迁移。
当 Realm 将可选值添加到所有属性时,您可以通过简单地显式将其明确设为必需来缓解这些问题,但在添加或删除属性时,只需要迁移即可。
除非属性中的数据会在迁移中丢失,除非您选择保留它们,运行进行 Realm 迁移就像简单地添加一个空块一样简单。
RLMRealmConfiguration *configuration = [RLMRealmConfiguration defaultConfiguration];
configuration.schemaVersion = 1; //Must be incremented by 1 each time the schema is changed.
configuration.migrationBlock = ^(RLMMigration *migration, uint64_t oldSchemaVersion) { };
[RLMRealmConfiguration setDefaultConfiguration:configuration];
这将使您下次调用 [RLMRealm defaultRealm]
时,迁移将 运行 并且您的文件将被升级。