2 回答

TA貢獻1798條經驗 獲得超7個贊
這樣做的原因是因為 firebase 是異步執行的,而您在編碼時就好像這一切都是同步發生的一樣。這是什么意思 ?這意味著 firebase 調用不會立即從服務器返回值,獲取該值需要一些時間,但是在響應返回之前您的其余代碼正在執行,這就是您遇到問題的原因。所以,即使這段代碼執行:
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);
已經完成并被調用,然后你永遠不會從 firebase 獲得實際值。
這是你可以做的事情:
interface Callback{
? ? ? ? void firebaseResponseCallback(String result);//whatever your return type is.
? ? }
將此方法的簽名更改為如下所示:
public void getCaseNumber(Callback callback) {?
將您的代碼更改為此:
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);
? ? ? }
? ? ?});
}?
然后,當你打電話時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
? ? ? ? }
? ? });
}

TA貢獻1815條經驗 獲得超6個贊
您在該代碼中犯了一個錯誤。根據 firebase 最新版本,您只能在 .addOnCompleteListener/.addValueEvent/etc 函數中使用 firebase 數據庫的值/字符串。
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////////////////////////
}
});
}
在 firebase 外部添加完整監聽器或添加值監聽器,變量或對象值不會改變或保持不變!
添加回答
舉報