我有一个包含两个对象的模型: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,而是向每个chat
添加了SFCHMessage
属性,并使用RLMLinkingObjects
从它们链接回来。
看起来更改为(readonly)
被视为删除领域中的属性,而删除它被视为添加属性。我是否真的需要一个迁移块用于我的案例?我将如何执行此迁移?
答案 0 :(得分:1)
是的,我说它适合您使用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]
时,将运行迁移并升级您的文件。