0

我有一个包含两个对象的模型: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.messagesSFCHMessage.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已被弃用。因此,我没有向 RLMArray添加SFCHMessage对象,而是向 each 添加了一个属性,并使用.SFCHChat.messageschatSFCHMessageRLMLinkingObjects

看起来更改为(readonly)被视为删除领域中的属性,而删除它被视为添加属性。我真的需要一个迁移块来处理我的案例吗?我将如何执行此迁移?

4

1 回答 1

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]时,将运行迁移并升级您的文件。

于 2017-01-10T02:49:19.520 回答