Skip to content Skip to sidebar Skip to footer

Firebase Handling Disconect To Database

I'm not sure it's disconect or DatabaseErrors event. First I have a dialog show when start loading data on Firebase, and then I want to dismiss that dialog in two case : have int

Solution 1:

onCancelled() is called when the server rejects the listener, typically when the user doesn't have permission to access the data.

You'll probably want to prevent attaching the listener if you don't have a connection to the Firebase Database. For that you can listen to .info/connected and only attach the listener when that is true.

mDatabase = FirebaseDatabase.getInstance().getReference();
DatabaseReference connectedRef = mDatabase.child(".info/connected");
connectedRef.addValueEventListener(newValueEventListener() {
  @OverridepublicvoidonDataChange(DataSnapshot snapshot) {
    boolean connected = snapshot.getValue(Boolean.class);
    if (connected) {
        mDatabase.child(ReferenceToFirebase.CHILD_CATEGORIES)
            .addListenerForSingleValueEvent(newValueEventListener() {
                @OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
                    //do something//then dismiss dialog
                    mDialog.dismiss();
                }

                @OverridepublicvoidonCancelled(DatabaseError databaseError) {
                    System.err.println("Listener was cancelled");
                    mDialog.dismiss();
                }
        });
    } else {
      System.out.println("not connected");
      mDialog.dismiss();
    }
  }

  @OverridepublicvoidonCancelled(DatabaseError error) {
    System.err.println("Listener was cancelled");
  }
});

Post a Comment for "Firebase Handling Disconect To Database"