Realm Swift模型是否分开? [英] Realm Swift Models separate or not?

查看:83
本文介绍了Realm Swift模型是否分开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iOS和Swift世界的新手,我正在开发一个新的应用程序,我想使用Realm进行持久化。我的代码中已经有实体,我的服务访问并填充了HTTP API端点。

I'm new to the world of iOS and Swift and am working on a new app which I want to use Realm for persistence. I have Entities in my code already which my Services access and populate for an HTTP API endpoint.

现在我想坚持某些实体,并想知道我是否应该为我的每个实体创建新的Realm特定模型来从Realm读取和写入。或者我应该将所有现有的普通Swift实体转换为Realm实体。起初这感觉不对,因为我会在我的应用程序中传递Realm Entities,而不仅仅是在持久层中。

Now I want to persist certain Entities and wanted advice as to whether I should create new Realm specific Models for each of my entities to read and write from Realm. Or should I convert all my existing plain Swift Entities to Realm Entities. At first this felt wrong as I would be passing Realm Entities al around my app instead of just in the persistence layer.

然而,另一种选择是,每当我向Realm读取/写入实体时,我需要将它们从实体转换为Realm实体。

However, the alternative is that every time I read/write entities to Realm I need to convert them back and forth from Entities to Realm Entities.

有关最佳方法的建议吗?

Any advice on the best approach to this?

谢谢

推荐答案

两种策略都很好并且各有优势和缺点。

Both strategies are fine and have their own advantages and disadvantages.


  • ✅值对象是线程-safe

  • ✅值对象可以在任何地方变异,而不用担心副作用

  • ✅可以任意定义值对象并允许使用语言的全部可能性,允许解决由对象持久性映射给出的约束

  • ❗️没有延迟加载,这意味着必须将完整的对象层次结构加载到内存中

  • ❗️无法表达周期

  • ❗️需要维护模型定义两次

  • ❗️需要逻辑到从传输编码(例如JSON)到Swift Structs以及从那些到Realm对象的映射

  • ✅ Value objects are thread-safe
  • ✅ Value objects can be mutated anywhere, without worrying about side-effects
  • ✅ Value objects can be arbitrary defined and allow to use the full possibilities of the language, which allows to workaround constraints given by object persistence mapping
  • ❗️ No lazily-loading, which means the full object hierarchy has to be loaded into memory
  • ❗️ Can't express cycles
  • ❗️ Requires to maintain your model definitions twice
  • ❗️ Needs logic to map from transport encoding (e.g. JSON) to Swift Structs and from those to Realm Objects

  • ✅零复制,whic h意味着从中读取更便宜

  • ✅Live,这意味着他们的数据始终是最新的

  • ✅从数据库中懒洋洋地加载:更少磁盘读取

  • ✅可以表达周期和任意对象层次结构

  • ✅在一个地方定义模型

  • ✅如果您可以控制传输编码并且可以共享命名约定,那么您可以主要依赖于 create(_:update:_)使用的Realm的集成Foundation值类型映射逻辑

  • ✅支持KVO,允许与一些反应式编程框架轻松集成

  • ❗️为你定义你的方式添加约束模型对象(有些语言结构不直接作为枚举支持,现在需要解决方法)

  • ❗️引用类型需要更多关注突变以避免不良副作用,此外修改只是在写入交易中可能存在( d被批量尽可能大)

  • ❗️领域对象不是线程安全的

  • ✅ Zero-copy, which means reading from them is less expensive
  • ✅ Live, which means their data is always up to date
  • ✅ Lazily loaded from the database: fewer disk reads
  • ✅ Can express cycles and arbitrary object hierarchies
  • ✅ Define your model in one place
  • ✅ If you can control the transport encoding and you can share naming conventions, you could rely mostly on Realm's integrated Foundation value type mapping logic used by create(_:update:_) and its friends.
  • ✅ Supports KVO, which allow easy integration with some reactive programming frameworks
  • ❗️ Adds constraints to the way how you define your model objects (Some language constructs are not directly supported as enums and require workarounds for now)
  • ❗️ Reference types need more care for mutations to avoid undesirable side-effects, in addition modifications are only possible within write transactions (which should be batched to be as large as possible)
  • ❗️ Realm Objects are not thread-safe

在选择退出Realm对象时会丢失许多Realm功能,并且很难重新实现它们。根据您需要多少以及您的用例如何,您将以高成本购买线程安全。

You lose many Realm features when opting out of Realm Objects and will have a hard-time to re-implement them yourselves. Depending on how much you need these and how your use-case looks like, you would buy thread-safety for a high cost.

从长远来看,我们是致力于使Realm对象更容易使用并尝试消除它们的缺点,因此长时间意识到这些对于做出明智决定有什么帮助。

In the long-term we're working on making Realm objects even easier to use and try to eliminate their disadvantages, so long being aware what those are will be helpful for making a well-informed decision.

这篇关于Realm Swift模型是否分开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆