简体   繁体   English

我们可以有两个领域吗?

[英]Can we have 2 Realms?

I just discovered Realm. 我刚刚发现了Realm。 I'm thinking of an architecture design where there's an iOS app, a Mac app, and a backend. 我正在考虑一种架构设计,其中有一个iOS应用程序,一个Mac应用程序和一个后端。 The iOS app has a Realm (Realm Mobile Platform) that is connected to a Realm in the backend. iOS应用程序具有一个Realm(Realm移动平台),该Realm在后端连接到Realm。 The iOS app has another Realm (Realm Mobile Database). iOS应用程序具有另一个Realm(Realm移动数据库)。 The backend has a database that stores files. 后端有一个存储文件的数据库。

Here's what I'm thinking. 这就是我的想法。

The iOS app gets some JSON from a third party API, parses it and compares it to the stuff in the Realm (the one connected to the backend). iOS应用程序从第三方API获取一些JSON,将其解析并将其与Realm中的内容(连接到后端的内容)进行比较。 The stuff that's not in the Realm, gets sent to the Mac app, which will download the corresponding files from an external source, and then upload those files to the database. 不在领域中的内容将发送到Mac应用程序,该应用程序将从外部源下载相应的文件,然后将这些文件上传到数据库。 The Mac App then alerts the iOS app, which then puts those things into the Realm (the one connected to the backend). 然后,Mac App提醒iOS应用程序,然后将这些东西放入Realm(连接到后端的东西)中。 This is the 'update the backend' process. 这是“更新后端”过程。

Now, the iOS app compares both of its Realms together, and gets the stuff that isn't in the local Realm. 现在,iOS应用将两个领域进行比较,并获得本地领域中没有的内容。 It then downloads the files from the database using the links in those realm objects. 然后,使用这些领域对象中的链接从数据库下载文件。 Once they're downloaded, it adds those objects to its local Realm store. 下载它们之后,它将这些对象添加到其本地Realm存储中。 This is the 'sync the device with the backend' process. 这是“与后端同步设备”过程。

Both Realms would be storing the same kind of object. 两个领域都将存储相同类型的对象。

Extra: 额外:

Ideally, the device doesn't have to be in perfect sync with the database, and so will most likely have some subset of the backend Realm and the database files. 理想情况下,设备不必与数据库完美同步,因此很可能会有后端Realm和数据库文件的某些子集。

Is this combination of one Realm Platform and one Realm Database possible? 一个Realm平台和一个Realm数据库的组合是否可能?

Yep! 是的! That should be possible. 那应该是可能的。 You can have any number of both synchronized and unsynchronized Realms in an app; 一个应用程序中可以有任意数量的同步领域和非同步领域。 you just need to make sure to coordinate the Configuration objects you use to instantiate them. 您只需要确保协调用于实例化它们的Configuration对象即可。

It's possible to easily copy Realm objects from one Realm to another: 可以轻松地将Realm对象从一个Realm复制到另一个Realm:

let objectFromLocalRealm = //...
let synchronizedRealm = //...

try! synchronizedRealm.write {
   sychronizedRealm.create(*objectType*.self, value: objectFromLocalRealm, update: true)
}

If you use this method though, you need to make sure both the object itself, and any child objects in List s or otherwise have primary keys, or you can end up with multiple copies of the same object in the synchronized Realm. 但是,如果使用此方法,则需要确保对象本身以及List的任何子对象都具有主键,否则您可能会在同步的领域中获得同一对象的多个副本。

Realm's test for equality is it checks that two Object instances are point to the same backing row in the database, so comparing two objects from different Realms might not work automatically. Realm的相等性测试是检查两个Object实例是否指向数据库中的同一后备行,因此比较来自不同Realms的两个对象可能无法自动进行。 You may need to implement a manual comparison method that checks that certain property values match. 您可能需要实现一种手动比较方法,以检查某些属性值是否匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM