Skip to content Skip to sidebar Skip to footer

Wait For OnComplete Callback

I'm trying to return a boolean value if the barcode exists. However, the way this function is currently setup, it always return false. It doesn't wait on the onComplete callback. I

Solution 1:

Create callback listener, like below

public interface OnCompleteCallback{
  void onComplete(boolean success);
}

Modify method to pass callback

public void barcodeExists(final String barcode,final OnCompleteCallback callback) {
    DocumentReference barcodeRef = mFireStore.collection("xyz")
            .document(barcode);

    barcodeRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
            if(task.isSuccessful()) {
                DocumentSnapshot document = task.getResult();
                callback.onComplete(document.exists());
            }
        }
    });
}

Final call of barcodeExists

barcodeExists("key", new OnCompleteCallback(){
  public void onComplete(boolean success){
     // do something
  }
});

Post a Comment for "Wait For OnComplete Callback"