菜鸟教程小白 发表于 2022-12-13 12:04:10

ios - 如何在 Realm 中实现继承(iOS,Objective C)


                                            <p><p>我有一个实体:</p>

<ul>
<li>'Person' - 抽象类。 </li>
<li>'Contact' - Person 的子类</li>
<li>'User' - Person 的子类</li>
<li>'Activity' - 包含一个 relatedPerson 作为 Person 类型,但它可以是 Contact 或 User。</li>
</ul>

<p>我的问题:当我缓存“事件”时,Realm 将相关人员缓存为“人员”类型,并丢失了“用户”或“联系人”的所有数据。如何解决这个问题? </p>

<p>我的类(class)结构是</p>

<pre><code>@interface Person : RLMObject

@property NSString *uID;

- (NSString *)displayName;
...
@end

@interface Contact : Person

@property NSString *fullName;
...
@end

@interface User : Person

@property NSString *nickname;
...
@end


@interface Activity : RLMObject

@property NSString *uID;
@property NSDate *createdAt;

@property Person *relatedPerson;

@end
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>Realm 尚不直接支持您所追求的类型的继承。在 <a href="https://github.com/realm/realm-cocoa/issues/1109" rel="noreferrer noopener nofollow">GitHub issue 1109</a> 中跟踪对它的支持.与此同时,您可以在 <a href="https://github.com/realm/realm-cocoa/issues/1109#issuecomment-143834756" rel="noreferrer noopener nofollow">@jpsim</a> 发表的该问题的评论中看到一些模拟继承的不同方法。和 <a href="https://github.com/realm/realm-cocoa/issues/1109#issuecomment-143985250" rel="noreferrer noopener nofollow">@mrackwitz</a> .哪种方法最适合您将取决于您的模型的要求。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何在 Realm 中实现继承(iOS,Objective C),我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/33758361/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/33758361/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何在 Realm 中实现继承(iOS,Objective C)