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_numbe
There is a mistake you are doing in that code. According to firebase latest Version, You can use the value/ String of firebase database only inside the .addOnCompleteListener/.addValueEvent/etc Function.
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()) {
/////////////////////////////call here everything related to it////////////////////////
}
});
}
Outside the firebase add complete listener or add value listener, The variable or object value will not change or remain same!
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