菜鸟教程小白 发表于 2022-12-11 19:55:49

ios - Realm :多个实例,每个实例都有自己的配置


                                            <p><p>我的应用程序使用两个独立的 Realm 实例(第二个来自库)。 </p>

<p>应用程序本身使用 <code>Realm.Configuration.defaultConfuguration</code> 并且库创建自己的配置 (<code>Realm.Configuration(...)</code>)。</p>

<p>在运行时(使用 Realm Browser 检查后),我们看到两个实例(每个实例都存在于自己的文件中)包含来自两个 Realm 的模型。当然,这对迁移有影响。 </p>

<p>我知道当两者使用相同的配置时,我们应该设置 <a href="https://realm.io/docs/swift/latest/#class-subsets" rel="noreferrer noopener nofollow">configuration.objectTypes</a> ,但我没想到每个实例都有自己的配置时这很重要。</p>

<p>两个不同的配置如何在它们之间共享任何数据?这似乎是 Realm 中的一个错误 - 或者我可能遗漏了什么。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>解释是<a href="https://github.com/realm/realm-cocoa/issues/5707#issuecomment-376927851" rel="noreferrer noopener nofollow">posted</a>在 GitHub 上 Realm 的问题中。我在此处复制响应以供将来搜索:</p>

<blockquote>
<p>By default <code>objectTypes</code> will include all <code>RealmSwift.Object</code>
subclasses, regardless of where they are defined.</p>

<p>A library which uses Realm should override
<code>shouldIncludeInDefaultSchema()</code> to exclude its types from the default
<code>objectTypes</code> (i.e. add <code>public override class func
shouldIncludeInDefaultSchema() -&gt; Bool { return false }</code> to the class
definitions) and then explicitly list the types it uses. This lets any
applications using the library continue to simply use the automatic
class discovery.</p>
</blockquote>

<p>功劳归<a href="https://github.com/tgoyne" rel="noreferrer noopener nofollow">Thomas Goyne</a> ( Realm 开发者)。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios -Realm :多个实例,每个实例都有自己的配置,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/49507690/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/49507690/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - Realm :多个实例,每个实例都有自己的配置