Realm Swift2:分离模型和Realm模型类的最佳实践 [英] Realm Swift2: Best practice for model and Realm model class separation

查看:100
本文介绍了Realm Swift2:分离模型和Realm模型类的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用Realm for Swift.阅读文档后,我仍然会想到几个问号.

I just have started working with Realm for Swift. After reading the documentation, still I have a couple of question marks in mind.

我最大的建议是,是否存在将模型类与领域类分离的最佳实践.

My biggest one is, if there is a best practice for separation Model classes from Realm classes.

例如,在Java MVC项目中,我们使用了负责与数据库层通信的DAO类(数据访问对象类). 我们相应的模型类只注入了dao对象,或者为此我们使用了服务类(例如CRUD操作).

For example, in Java MVC projects we used DAO classes (Data Access Object classes) which were responsible for the communication with the database layer. Our corresponding model classes only have either the dao object injected or we used service classes for this (like CRUD operations).

如果我是一个Realm模型"类,那么现在看来这一切都是合而为一的.但是在将对象持久保存到数据库之后,在UI层中更改对象的属性将导致

If I habe a Realm "Model" class, this seems now to be everything in one. But after having the object persisted to the database, changing attributes of the object in the UI-Layer results in

'试图在写事务之外修改对象-调用 首先在RLMRealm实例上执行beginWriteTransaction.'

'Attempting to modify object outside of a write transaction - call beginWriteTransaction on an RLMRealm instance first.'

不过,这让我回到了最初的想法:不应将它们分为Realm对象和模型对象.还是可以在View-Class中使用"realm.write"进程?

Which brings me back to my initial though: Shouldn’t this be separated in Realm objects and model objects. Or is it OK to have "realm.write" processes in View-Classes then?

我对此进行了一些研究,但结果对此非常有用.

I did some research on this but the results are very vage on this.

您如何在项目中处理此问题.您有某种最佳实践或指导吗?

How do you deal with this in your projects. Do you have some kind of best practice or guidance?

非常感谢 约翰

推荐答案

(至少在iOS方面),官方没有建立将实际的Realm Object子类抽象为模型类逻辑的最佳实践.话虽如此,我肯定听说过过去为支持多种类型的数据框架而执行这种逻辑的应用程序.

Officially, (on the iOS side at least), there's no established best practice for abstracting the model class logic away from the actual Realm Object subclasses. That being said, I've definitely heard of apps in the past who do do this sort of logic in order to support multiple types of data frameworks.

如果要执行此操作,我将为实现我自己的API的每个模型创建一个单独的对象类,以了解如何获取/设置数据属性,并将Realm对象设置为该对象的内部成员.然后,该对象将用作我的应用程序逻辑与如何将数据保存到Realm之间的通用接口.这样,如果我确实想在线下交换数据框架,则可以简单地用一个新的数据对象替换自己的数据对象,但保持API的一致性.

If I were to do this, I would make a separate object class for each model implementing my own API on how to get/set data properties, and make the Realm object an internal member of this object. This object would then serve as the generic interface between my app's logic and how to save that data to Realm. That way, if I did want to swap out the data framework down the line, I could simply replace my own data object with a new one, but keep the API consistent.

关于您发布的错误消息,为了确保数据完整性,除非它在写事务中,否则您不能修改Realm对象(UI线程或其他).话虽如此,您可以在该抽象对象中非常容易地封装该逻辑(即在当前线程上打开Realm写事务).

In regards to that error message you posted, in order to ensure data integrity, you cannot modify a Realm object (UI thread or otherwise) unless it's in a write transaction. That being said, you could encapsulate that logic (i.e. open up a Realm write transaction on the current thread) pretty easily within that abstract object.

这篇关于Realm Swift2:分离模型和Realm模型类的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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