Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android check firebase connection

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?

Below is my code:

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

Thank you for you answers

like image 895
Jean-Philippe Mary Avatar asked Aug 19 '17 13:08

Jean-Philippe Mary


1 Answers

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.

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.

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

like image 109
Frank van Puffelen Avatar answered Sep 18 '22 17:09

Frank van Puffelen