亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用全局變量從內部函數獲取空字符串

使用全局變量從內部函數獲取空字符串

RISEBY 2023-05-10 13:37:55
請幫我解決一些小問題,我相信你能做到:D我正在嘗試在帶有字段“case_number”的 firestore 文檔“user_cases_information”上設置一個字段首先我聲明這個全局變量private String case_number_consecutive = new String("");在.setOnClickListener我有這個:(從其他文件中讀取舊的連續或案例編號)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)它假設當我檢查 firestore 時,正是我設置“user_cases_information”文檔的文檔,我應該看到數字或新的 case_number,但我總是得到一個參考,連續的字段總是null=null為什么我得到這個空值?我用了一個 Toast 并把它放在這之前:Toast.makeText(...."Value: "+case_number_consecutiveMap<String, Object> user_cases_information = new HashMap<>();user_cases_information.put("case_number", case_number_consecutive);它顯示了null上述函數中的值或數據正在丟失我怎樣才能解決這個問題???萬分感謝。
查看完整描述

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

? ? ? ? }

? ? });

}


查看完整回答
反對 回復 2023-05-10
?
紅糖糍粑

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 外部添加完整監聽器或添加值監聽器,變量或對象值不會改變或保持不變!


查看完整回答
反對 回復 2023-05-10
  • 2 回答
  • 0 關注
  • 143 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號