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

publicinterfaceOnCompleteCallback{
  voidonComplete(boolean success);
}

Modify method to pass callback

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

    barcodeRef.get().addOnCompleteListener(newOnCompleteListener<DocumentSnapshot>() {
        @OverridepublicvoidonComplete(@NonNull Task<DocumentSnapshot> task) {
            if(task.isSuccessful()) {
                DocumentSnapshotdocument = task.getResult();
                callback.onComplete(document.exists());
            }
        }
    });
}

Final call of barcodeExists

barcodeExists("key", newOnCompleteCallback(){
  publicvoidonComplete(boolean success){
     // do something
  }
});

Post a Comment for "Wait For Oncomplete Callback"