实施针对 Firebase 实时数据库读取操作的“失败状态”。

huangapple go评论80阅读模式
英文:

Implementing a "fail state" for a Firebase Realtime Database read operation

问题

好的,以下是您提供的内容的中文翻译:

假设应用程序在实时数据库(RTDB)上执行了一个简单的读取操作,可能类似于这样:

Firebase.database.reference.child("foo").addListenerForSingleValueEvent(object: ValueEventListener {
    override fun onCancelled(p0: DatabaseError) = Unit
    override fun onDataChange(snap: DataSnapshot) {
        //....
    }
})

假设连接尚未建立,可能是因为用户首先未连接到互联网,或者他们失去了连接,并且从“Fragment”中启动了读取操作。因此,除非用户重新连接到互联网并且读取成功,或者他们重新启动应用程序并且读取请求被移除(在禁用离线持久性的情况下),读取请求将保持存在,应用程序将尝试连接到Firebase RTDB以获取数据。

因此,我的问题是,是否有一种方法可以在一定的时间后“取消”读取请求,也许可以使用“delay”协程,在一定的延迟时间之后,“取消”读取请求,并在通知用户检查他们的连接或类似情况后仅在再次初始化读取请求。

英文:

Let's say the app performs a simple read operation on the RTDB, maybe something like this:

Firebase.database.reference.child("foo").addListenerForSingleValueEvent (object: ValueEventListener {
    override fun onCancelled(p0: DatabaseError) = Unit
    override onDataChange(snap: DataSnapshot) {
        //....
    }
})

Let's say the connection has not been established, maybe because the user is not connected to the Internet, to begin with, or they lost the connection, and a read is initiated from a Fragment, so unless the user reconnects to the Internet and the read succeeds or they restart the app and the read request is removed (in the case where offline persistence is disabled), the read request will remain and the app will try to connect to the Firebase RTDB to fetch the data.

So my question here is, is there a way to "cancel" a read request after a certain amount of time passes, maybe by using the delay coroutine and after a certain delay period, "canceling" the read request, and initiating it again only after notifying the user to check their connection or something similar.

答案1

得分: 1

> 因此,在使用 addListenerForSingleValueEvent() 附加 ValueEventListener 一段时间后,如果尚未获取到值,可以使用 removeEventListener()

首先,当您使用 addListenerForSingleValueEvent() 时,意味着您只获取数据一次,这种情况下不需要删除任何监听器。但是,当使用 addValueEventListener() 时,意味着您在实时中监听更改,这种情况下您需要根据您的活动生命周期相应地删除监听器

因此,如果您需要这种行为,可以在一段时间后删除监听器,这意味着您将停止监听更改。在这种情况下,不会获取任何数据。如果以后需要获取数据,只需重新附加监听器并开始监听更改。

还有一个可能会对您有用的库,称为Firebase-UI。此库可以帮助您更轻松地管理这种情况。

英文:

> So after a certain time has passed since attaching the ValueEventListener using addListenerForSingleValueEvent() , and the value hasn't been fetched yet, if removeEventListener() is used

First of all, when you are using addListenerForSingleValueEvent(), it means that you are getting the data exactly once, case in which there is no listener that needs to be removed. However when using addValueEventListener(), it means that you are listening for changes in real-time, case in which you need to remove the listener accordingly to the life-cycle of your activity.

So if you need that behavior, you can remove the listener after a period of time, meaning that you'll stop listening for changes. In this case, not data will be fetched. If you need later to get the data, simply attach the listener again and start listening for changes.

There is also a library that you might be interested in, which is called Firebase-UI. This library can help you manage that kind of situation more easily.

huangapple
  • 本文由 发表于 2020年9月7日 16:01:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63773554.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定