[英]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. 您可以使用Realm
的object(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.