0

I have a problem with assigning a value to a variable out of the OnSuccessListener. As you see in the code, first, I'm trying to write the value of abc into log inside the Listener and then outside I debug the value of the same string after the listener completed work, but in a log file I see first "after: null" and only after that "result = testField".

I suppose, that the point is that the listener does the task asynchronously, but I don't know when it finishes. I 'fixed' the problem using Thread.sleep, but it isn't the solution. Does anyone know the correct method of assgning the field value to a variable?

    String abc; 
    private void getInfo(String uid) {     
       DocumentReference doc = FirebaseFireStore.getInstance().document("sb/base");
       store.collection("users").document(uid).get().addOnSuccessListener(
                new OnSuccessListener<DocumentSnapshot>() {
            @Override
            public void onSuccess(DocumentSnapshot documentSnapshot) {
                if (!documentSnapshot.exists())
                    log("onSuccess: EMPTY");
                else {
                    abc = new JSONObject(documentSnapshot.getData()).toString();
                    log("result = " + abc);
                }
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(applicationManager.activity.getApplicationContext(),
                        "ERROR", Toast.LENGTH_SHORT).show();
            }
        });
      log("after: " + abc);
    }

1 Answers1

0

You use an interface which is common in this type of operations. I have modified your code to demonstrate the concept. You will use the code in this way:

LoadDBData dataBaseAccessOp=new LoadDBData();
//YOUR original function next
dataBaseAccessOp.getInfo("uidSelected", new LoadingListener(){
  @Override
  void OnSuccess(String data){
     log("result = " + data);
  }

  @Override
  void OnFail(){
    log("result = ERROR");
    Toast.makeText(applicationManager.activity.getApplicationContext(), 
          "ERROR", Toast.LENGTH_SHORT).show();
  }
});

And next below is the modified code.

Kf

public class LoadDBData {

  public interface LoadingListener{
     void OnSuccess(String data);
     void OnFail();
  }

  private void getInfo(String uid, LoadingListener usrListener) {         
    DocumentReference doc = FirebaseFireStore.getInstance().document("sb/base");
    store.collection("users").document(uid).get().addOnSuccessListener(
      new OnSuccessListener<DocumentSnapshot>() {
      @Override
        public void onSuccess(DocumentSnapshot documentSnapshot) {
        if (documentSnapshot.exists()==true)        
          String abc = new JSONObject(documentSnapshot.getData()).toString();
          usrListener.OnSuccess(abc);

        }
      }
    }
    ).addOnFailureListener(new OnFailureListener() {
      @Override
        public void onFailure(@NonNull Exception e) {
          usrListener.OnFail();        
      }
    }
    );
  }
}
K F
  • 1,368
  • 13
  • 20