从Android UI中抽象Firebase [英] Abstract Firebase from Android UI

查看:123
本文介绍了从Android UI中抽象Firebase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们将Firebase作为实时数据库连接到Activity或Fragment时,我面临的问题是Firebase作为一个很好的工具。但是试图从视图元素中分离并为其创建抽象(因为例如我们想在测试或将来替换它)变得相当困难,特别是试图实现MVP模式。



有没有解决这个问题的方法?

解决方案

Firebase和UI之间没有固有的耦合(除非您使用 FirebaseUI )。典型的解决方案是在某种Service / DAO类中封装Firebase数据库访问(例如使用Dagger注入到UI类中),并且在该类中的方法返回RxJava Observable(这也很好地使得可以嵌套firebase查询....例如,如果做一些相当于加入)

I'm facing with issue that Firebase as Realtime Database works great while we connect it with Activity or Fragment. But trying to separate from view elements and create some abstraction for it (because e.g. we want to replace it while testing or in the future) becomes quite hard, especially trying to implement MVP pattern.

Are there any solutions for this issue?

解决方案

There should be no inherent coupling between Firebase and UI (unless you're using FirebaseUI). Typical solution would be to encapsulate firebase database access in some kind of Service/DAO class (injected in to UI classes using Dagger for example)....and have methods in that class return RxJava Observable (this also nicely enables making sets of nested firebase queries....for example if doing something equivalent to a "join")

这篇关于从Android UI中抽象Firebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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