Getting null string from inner function with global variable

yuli castaño :

pls help me with little issue, im sure that you can do it :D

Im trying to set up a field on a firestore document "user_cases_information" with a field "case_number"

  1. First i declare this global var

private String case_number_consecutive = new String("");

  1. in .setOnClickListener i have this: (to read a old consecutive or case number from other document)
if (service_type.equals("support")){
    DocumentReference docRef = firestore_popup.collection("param_data_app").document("support_case_numbers");
    docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
     @Override
     public void onComplete(@NonNull Task<DocumentSnapshot> task) {
            if (task.isSuccessful()) {
                 DocumentSnapshot document = task.getResult();
                 String consecutive = document.get("consecutive").toString();
                    if (consecutive.equals(null)) {
                         int random = new Random().nextInt(117) + 4;
                         case_number_consecutive = "IOC-09"+random;
                    } else {
                        case_number_consecutive = consecutive;                                        UpdateCaseNumbersConsecutive("support");
                    }
             } else {
                  int random = new Random().nextInt(117) + 4;
                  case_number_consecutive = "IOC-09"+random;
              }
      }
     });
} 


Map<String, Object> user_cases_information = new HashMap<>();
user_cases_information.put("case_number", case_number_consecutive);
... (then i use a firestore reference to put the new case number)

It supposed that when i check firestore, precisely the document where i set the "user_cases_information" document, i should see the number or the new case_number, but i always get a null reference, the field consecutive is always = null

Why im getting this null? i used a Toast and put it before this:

Toast.makeText(...."Value: "+case_number_consecutive
Map<String, Object> user_cases_information = new HashMap<>();
user_cases_information.put("case_number", case_number_consecutive);

and it show null so the value or data is losing inside the above functions

how can i fix this??? thank you a lot.

a_local_nobody :

The reason for this, is because firebase is executing asynchronously, and you are coding as if it all happens synchronously. What does this mean ? It means that the firebase call does not immediately return a value from the server, it takes some time to get that value, but the rest of your code is executing before the response comes back, and that's why you're having problems. So, even though this code executes:

if (service_type.equals("support")){
    DocumentReference docRef = firestore_popup.collection("param_data_app").document("support_case_numbers");
    docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
     @Override
     public void onComplete(@NonNull Task<DocumentSnapshot> task) {
            if (task.isSuccessful()) {
                 DocumentSnapshot document = task.getResult();
                 String consecutive = document.get("consecutive").toString();
                    if (consecutive.equals(null)) {
                         int random = new Random().nextInt(117) + 4;
                         case_number_consecutive = "IOC-09"+random;
                    } else {
                        case_number_consecutive = consecutive;                                        UpdateCaseNumbersConsecutive("support");
                    }
             } else {
                  int random = new Random().nextInt(117) + 4;
                  case_number_consecutive = "IOC-09"+random;
              }
      }
     });
} 

by the time a response comes back, your other code :

Map<String, Object> user_cases_information = new HashMap<>();
user_cases_information.put("case_number", case_number_consecutive);

has already been completed and called, and then you never get the actual value from firebase.

have a look at my answer here : How to add results of Facebook Graph api to array list in Java and I'll help you make something similar if you need help

here's something you can do :

interface Callback{
        void firebaseResponseCallback(String result);//whatever your return type is.
    }

change the signature of this method, to be something like this :

public void getCaseNumber(Callback callback) { 

change your code to this:

if (service_type.equals("support")){
    DocumentReference docRef = firestore_popup.collection("param_data_app").document("support_case_numbers");
    docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
     @Override
     public void onComplete(@NonNull Task<DocumentSnapshot> task) {
            if (task.isSuccessful()) {
                 DocumentSnapshot document = task.getResult();
                 String consecutive = document.get("consecutive").toString();
                    if (consecutive.equals(null)) {
                         int random = new Random().nextInt(117) + 4;
                         case_number_consecutive = "IOC-09"+random;
                    } else {
                        case_number_consecutive = consecutive;                                        UpdateCaseNumbersConsecutive("support");
                    }
             } else {
                  int random = new Random().nextInt(117) + 4;
                  case_number_consecutive = "IOC-09"+random;
              }

       callback.firebaseResponseCallback(case_number_consecutive);
      }
     });
} 

then, when you call getCaseNumber:

getCaseNumber(new Callback() {
        @Override
        public void firebaseResponseCallback(String result) {
            here, this result parameter that comes through is your api call result to use, so result will be your case number, so make your objects inside this method, not outside
        }
    });
}

Edit

I've made this post specifically for these types of questions : How to get data from any asynchronous operation in android

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=129989&siteId=1