DEEz
DEEz

Reputation: 191

is there a possible way to catch no connection or timeout Exception in firestore?

I am making my app that when it firestore tries to communicate with the server then when it fails it will display to the user that he has no connection

first of all this code checks if the id that came from google Auth is already registered to the firestore

public FirebaseHelper CheckAccount(String id, final OnCheckAccountListener checkAccountListener){
    DocumentReference docIdRef = db.collection(Values.users).document(id);
    docIdRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if (task.isSuccessful()){
                    DocumentSnapshot document = task.getResult();
                    checkAccountListener.AccountExist(document.exists());
                }else{
                    if (task.getException().equals(FirebaseFirestoreException.Code.UNAVAILABLE)){
                        //Shows user that he/she is offline
                        checkAccountListener.onNoConnection();
                    }else {
                        //other catastrophic error
                        checkAccountListener.onError(task.getException().getMessage());
                    }
                }
        }
    });
    return this;
}
public interface OnCheckAccountListener{
    void AccountExist(boolean itExists);
    void onNoConnection();
    void onError(String Error);
}

now when i disconnect my internet then auth my account it will check the account then task.isSuccessful() is false and instead of displaying no connection like this

no connection dialog

it shows this

error dialog

and as you can see the reason is client is offline

there is a possible solution but its not nice

the solution is just

if(task.getException().getMessage().contains("offline")

sorry if bad explanation if you need more details just reply thx!

and if your wondering why there is an interface in the code because it runs in different classes just to neat things up! and the pictures are customdialog and i also made a loading dialog like this!

enter image description here

all image in the dialog are GIF's and they are awesome!

Upvotes: 0

Views: 1034

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317372

Firestore does not have connection errors that you can catch. The SDK will automatically initiate and retry broken connections in the background for as long as your app process is alive. You don't have any direct control over how this works.

For a list of error codes that you can get by handling an FirebaseFirestoreException object, read the API documenatation.

Upvotes: 2

Related Questions