Android检查firebase连接 [英] Android check firebase connection

查看:90
本文介绍了Android检查firebase连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的Android应用程序使用Firebase数据库。
当我提交分数时,如果它起作用,则会出现一条消息(片段serverOK)。
在其他所有情况下,我也想留言。如果databaseEror不为null,那么有一个问题,但是如果它没有连接到Firebase怎么办?

I am using a firebase database for my Android app. When I submit a score there is a message (fragment serverOK) if it works. I would like a message for all the other cases. There is one if the databaseEror is non null but how to do if it didn't connect to firebase?

下面是我的代码:

ref.setValue(resultat, new DatabaseReference.CompletionListener() {
    @Override
    public void onComplete(DatabaseError databaseError, DatabaseReference dataRef) {
        if (databaseError == null) {
           FragmentManager fm3 = getFragmentManager();
           ServerOk serverok = new ServerOk();
           serverok.show(fm3, "serverok");
        } else {
           FragmentManager fm3 = getFragmentManager();
           Server server = new Server();
           server.show(fm3, "server");
        }
    }
});

谢谢您的回答

推荐答案

Firebase客户端不会将缺少连接的情况视为错误,因此不会使用错误调用完成侦听器。

The Firebase client doesn't treat the absence of a connection as an error, so it doesn't call your completion listener with an error.

当Firebase客户端未连接到后端时调用 setValue()时,客户端会将写入保留在队列中。恢复连接后,它将写操作发送到服务器。

When you call setValue() while the Firebase client is not connected to its backend, the client keeps the write in a queue. When the connection is restored, it sends the write to the server.

仅当服务器执行写操作后,您的完成处理程序才会被调用。如果发生错误(即如果安全规则拒绝了写操作),则会将错误传递给回调。

Your completion handler will only be called once the write has been executed by the server. If there was an error (i.e. if the security rules rejected the write), you will get an error passed into the callback.

如果您想知道客户端是否在调用 setValue()之前已连接到服务器,则可以将侦听器附加到 .info / connected

If you want to know if the client is connected to the server before calling setValue(), you can attach a listener to .info/connected.

这篇关于Android检查firebase连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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