java - 使用全局变量从内部函数获取空字符串

标签 java android firebase google-cloud-firestore

请帮我解决一些小问题,我确信你能做到:D

我正在尝试在 firestore 文档“user_cases_information”上设置一个字段,其中包含“case_number”字段

  1. 首先我声明这个全局变量

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_consecutive
    Map<String, Object> user_cases_information = new HashMap<>();
    user_cases_information.put("case_number", case_number_consecutive);
    

    并且它显示null,因此上述函数中的值或数据正在丢失

    我该如何解决这个问题???万分感谢。

    最佳答案

    这样做的原因是因为 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 获得实际值。

    看看我的答案:How to add results of Facebook Graph api to array list in Java如果您需要帮助,我会帮助您制作类似的东西

    您可以执行以下操作:

    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
            }
        });
    }
    
    <小时/>

    编辑

    我专门针对这些类型的问题撰写了这篇文章: How to get data from any asynchronous operation in android

    关于java - 使用全局变量从内部函数获取空字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59418348/

    相关文章:

    reactjs - 禁用帐户选择器 FirebaseUI React

    Android - Firebase 离线最佳实践

    Java Socket 编程 - HTTP 1.1 的 301 错误

    android - Android手机水平时确定滚动角度(上/下)?

    firebase - 是否可以在 firebase 中查看用户明智的使用统计信息?

    android - 在 Android Marshmallow 中遍历文件系统

    android - 方向改变事件或监听器 Android

    java - 将 JSONObject 发布到 Restful Web 服务时出现 400

    java - 更改 EHCache 3 中各个条目的 TTL

    java - 在java中转置(处理数组)一维数组