在 Android 应用程序中抽象 Realm 的正确方法 [英] Proper way to abstract Realm in Android apps

查看:21
本文介绍了在 Android 应用程序中抽象 Realm 的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Android 应用程序中尝试 Realm.io,但为了安全起见,我想抽象 DB 层,以便在需要时可以切换回基于标准的 SQLiteDB 无需重写大部分应用程序.

I'm trying out Realm.io in an Android app, though, to stay on the safe side, I would like to abstract the DB layer so that, in case of need, I can switch back to a standard SQLite based DB without rewriting most of the app.

然而,由于 Realm 的特殊性质,我发现它很难正确抽象:

I'm however finding it difficult to properly abstract Realm due to it's particular nature:

  • 当绑定到一个领域时,RealmObjects 是代理,所以我不能像 POJO 那样传递它们.
  • 所有 Realm 实例都需要为使用它们的每个线程正确打开和关闭.

我已经使用最近的 Realm.copyFromRealm() API 而不是传递绑定到 Realm 的 RealmObjects 来解决这些限制,但这样我认为我失去了使用 Realm 的所有好处(是吗?).

I've resorted to using the recent Realm.copyFromRealm() API instead of passing around RealmObjects tied to a Realm to get around these limitations but this way I think I'm loosing all the benefits of using realm (am I?).

有什么建议吗?

推荐答案

我会试着回答你的第一个困惑.真的没有必要通过意图传递 RealmObjects,或者换句话说,不需要它们是 parcelableserializable

I would try to answer your first confusion. There is really no need to pass around RealmObjects via intents, or in other terms no need of them being parcelable or serializable

您应该做的是通过 Intent 传递特定 primary id 或该特定 RealmObject 的其他参数,以便您可以在下一个活动中再次查询该 RealmObject.

What you should do is to pass around particular primary id or other parameter of that particular RealmObject via intent, so that you can query that RealmObject again in next activity.

例如,假设您在开始活动时传递了 primaryId,

For example suppose you pass primaryId while starting an activity,

Intent intent = new Intent(this, NextActivity.class);
intent.putExtra(Constants.INTENT_EXTRA_PRIMARY_ID, id);

现在在 NextActivity 中从意图中获取 id 并简单地查询 RealmObject,即,

Now inside NextActivity get id from intent and simply query RealmObject, i.e.,

int primaryId = getIntent().getIntExtra(Constants.INTENT_EXTRA_PRIMARY_ID, -1);
YourRealmObject mObject = realm.where(YourRealmObject.class).equalTo("id", primaryId).findFirst();

看,您在新活动中获得了对象,而不是担心传递对象.

See, you got your object in new activity, rather than worrying about passing the object around.

我不知道您在打开和关闭领域对象方面遇到了什么特殊问题.你尝试过 Realm 的 Transaction blocks 吗?如果你使用它,你就不需要依赖打开和关闭.

I don't know what particular problem you're getting into regarding opening and closing realm objects. Did you try Realm's Transaction blocks? You don't need to rely on opening and closing if you use that.

我希望这会有所帮助.

更新

既然你在寻找抽象,

只需创建一个类似 DbUtils 的类,并在其中设置一个类似 getYourObjectById 的方法,然后传递上面的 id 并检索您的对象作为回报.这在某种程度上使它变得抽象.然后,您可以保留该类和方法,如果您切换到另一个数据库解决方案,只需更改方法内容即可.

Just create a class like DbUtils and have a method there like getYourObjectById and then pass above id and retrieve your object in return. That makes it abstract in a way. You can then keep that class and method, and just change method content if you ever switch to another database solution.

这篇关于在 Android 应用程序中抽象 Realm 的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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