简体   繁体   English

跨多个领域的关系?

[英]Relationships across multiple Realms?

Realm: 领域:

We have the following scenario: There are several stores with employees and customers, several employees that could work at more than one store, and several customers that may shop at several stores. 我们有以下情形:有几家拥有员工和客户的商店,几名员工可以在一个以上的商店工作,以及几个客户可能在数个商店购物。 This could be represented with these classes 这可以用这些类来表示

class Store {
    dynamic var id = ""
    dynamic var address = ""
    let workers = List<Employee>()
    let customers = List<Customer>()
}

class Customer {
    dynamic var id = ""
    dynamic var name = ""
    let stores = LinkingObjects(fromType: Store.self, property: "customers") 
    // ... many more fields about this customer
}

class Employee {
    var id
    var name
    let work = LinkingObjects(fromType: Store.self, property: "workers")
}

The catch here is that we must protect customer information, so none of the customer info can be present in a shared realm and needs to be secure. 这里要注意的是,我们必须保护客户信息,因此,没有一个客户信息可以出现在共享领域中并且需要安全。 Neither the store nor Employee data is a security matter. 商店数据和员工数据都不是安全问题。 Our current approach is to give each customer their own realm, however, the major drawbacks to this is that requires massive duplication since each customer realm must copy the data of the store. 我们当前的方法是给每个客户自己的领域,但是,这样做的主要缺点是需要大量复制,因为每个客户领域都必须复制商店的数据。 The other drawback is that we would be copying customer data into a shared realm which is a security risk. 另一个缺点是我们会将客户数据复制到共享领域,这是安全隐患。 What would be the best way to architect this scenario that allows for relationships across different Realms? 构架允许跨不同领域的关系的方案的最佳方法是什么?

Realm doesn't currently support "direct" object links across Realms analogous to object properties within the same Realm. Realm当前不支持跨Realms的“直接”对象链接,类似于同一Realm中的对象属性。

Instead, what I suggest you do is to give your objects primary keys (you can probably just declare your existing id fields as such, or create a new internalId field if your existing id field can't be used for this purpose). 相反,我建议您为对象提供主键(您可以这样声明现有的id字段,或者如果不能将现有的id字段用于此目的,则可以创建一个新的internalId字段)。

Primary keys are mandatory, must be unique, and can't be changed after they are set, which makes them great for uniquely identifying objects. 主键是强制性的,必须是唯一的,并且在设置主键后不能更改,这使它们非常适合唯一标识对象。 Our documentation discusses them in greater detail. 我们的文档将对它们进行更详细的讨论。

Then, instead of directly storing customer info/a customer object in a shared Realm, you can just store the primary keys for the relevant customers, for example in a list. 然后,您可以直接将相关客户的主键存储在列表中,而不是直接在共享领域中存储客户信息/客户对象。 (Right now you'll have to make a wrapper CustomerKey object for example to store the customer's primary key, but we plan to support collections directly containing strings or other primitive types very soon.) (现在,您必须制作一个包装器CustomerKey对象,例如以存储客户的主键,但是我们计划很快支持直接包含字符串或其他原始类型的集合。)

You can enhance this further by adding helper methods on your objects that can be passed in a customer Realm and return the user object (or whatever object's primary key is being stored), looking it up in the Realm automatically. 您可以通过在对象上添加可以在客户领域中传递的辅助方法并返回用户对象(或存储对象的主键),然后在Realm中自动查找对象,来进一步增强此功能。 You can use Realm 's object(ofType:forPrimaryKey) method to look up an object based on its primary key. 您可以使用Realmobject(ofType:forPrimaryKey)方法根据对象的主键查找对象。

The main limitation is that you won't get the automatic updating of links you would get with object, list, and LinkingObjects properties. 主要限制是您不会自动获得与对象,列表和LinkingObjects属性相关的链接。 You'll have to manually perform the bookkeeping yourself. 您必须自己手动执行簿记。

If you have ideas for functionality you want to see in Realm that would go beyond what I've posted here, feel free to share your thoughts at our GitHub issue tracker . 如果您有想要在Realm中看到的功能性想法,而这些想法超出了我在此处发布的范围,请随时在我们的GitHub问题跟踪器中分享您的想法。 We welcome feature requests. 我们欢迎功能要求。

暂无
暂无

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

相关问题 跨多个用户配置权限和共享域 - Configuring Permissions and Sharing Realms across Multiple Users 具有多个数据模型的多个领域 - Multiple Realms with multiple data models Android +领域:从多个领域获取数据 - Android + Realm: fetch data from multiple realms 使用多个领域时,如何停止所有类出现在具有0个实例的所有领域中? - When using multiple realms how do I stop all classes appearing in all realms with 0 instances? 领域:在应用程序中使用一个或多个领域(以及一个或多个模式) - Realm: Use one or multiple realms in an app (and one or multiple schemas) 迁移多个领域-领域版本大于架构版本例外 - Migrating multiple realms - Realm Version Greater Than Schema Version exception 从 webRTC 客户端为 coTurn 提供多个领域/来源 - Providing multiple realms/origins to coTurn from webRTC client 安装后首次启动应用程序时,React Native中的多个领域无法正确查询领域对象服务器 - Multiple realms in React Native don't query realm object server correctly on first launch of app after install 将本地域转换为同步域:&#39;customSchema&#39;不可访问 - Converting local Realms to synced Realms: 'customSchema' is inaccessible 如果用户具有管理员权限,是否可以一次查询多个领域? 还是拥有一个可供多个用户同时写入的共享领域? - Is it possible to query multiple realms at once if a user has admin privileges? Or have a shared realm that multiple users can write to at once?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM