在Android应用中提取Realm的正确方法 [英] Proper way to abstract Realm in Android apps

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

问题描述

不过,为了安全起见,我正在Android应用程序中试用Realm.io,我想抽象数据库层,以便在需要时可以切换回基于SQLite的标准DB,而没有重写大部分应用程序。



但是我发现由于其特殊的性质,很难正确抽象Realm:




  • 绑定到领域时,RealmObjects是代理,因此我无法像POJO一样传递它们。

  • 所有Realm实例都必须正确



我一直使用最近的Realm.copyFromRealm()API代替绕过与Realm绑定的RealmObjects来解决这些限制,但是我想这样就失去了使用Realm的所有好处(是吗?)。



任何建议?

解决方案

我会尝试回答您的第一个困惑。
确实不需要通过意图传递 RealmObject ,或者换句话说,它们不需要是可拆分的 可序列化



您应该做的是绕过特定的主ID 或该特定 RealmObject 的其他参数(通过意图),以便您可以在下一个活动中再次查询该RealmObject。



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

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

现在在 NextActivity 内,从intent和只需查询 RealmObject ,即

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

看,您在新活动中获得了对象,而不必担心传递对象。 p>

我不知道您在打开和关闭领域对象时遇到什么特别的问题。您是否尝试过Realm的 Transaction块



我希望这会有所帮助。



更新



由于您正在寻找抽象,因此



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


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.

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

  • When tied to a realm, RealmObjects are proxies so I cannot pass them around as they were POJOs.
  • All Realm instances need to be properly opened and closed for every thread they are used in.

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?).

Any suggestions?

解决方案

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

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.

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);

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.

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.

I hope this helps.

Update

Since you're looking for abstraction,

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天全站免登陆