问题描述
pls帮我解决了一些问题,我确定您可以做到:D
pls help me with little issue, im sure that you can do it :D
我正在尝试在Firestore文档"user_cases_information"上设置一个带有"case_number"字段的字段
Im trying to set up a field on a firestore document "user_cases_information" with a field "case_number"
- 首先,我声明此全局变量
private String case_number_consecutive = new String("");
- 在
.setOnClickListener
中我有这个:(从其他文档中读取旧的连续号或案例号)
- 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)
假设我在检查firestore时,恰好是我设置"user_cases_information"文档的文档,应该看到数字或新的case_number,但是我总是得到一个null
引用,连续的字段始终为= null
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);
并且显示null
,因此上述函数中的值或数据丢失了
and it show null
so the value or data is losing inside the above functions
我该如何解决???非常感谢.
how can i fix this??? thank you a lot.
推荐答案
之所以这样做,是因为firebase是异步执行的,并且您正在编码,就好像一切都同步发生一样.这是什么意思 ?这意味着firebase调用不会立即从服务器返回一个值,需要花费一些时间来获取该值,但是其余的代码在响应返回之前正在执行,因此这就是您遇到问题的原因.因此,即使执行以下代码:
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);
已经完成并被调用,然后您再也无法从firebase中获得实际值.
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
您可以执行以下操作:
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) {
将代码更改为此:
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
}
});
}
修改
我专门针对以下类型的问题发布了这篇文章:如何从任何异步操作中获取数据在android
I've made this post specifically for these types of questions :How to get data from any asynchronous operation in android
这篇关于使用全局变量从内部函数获取空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!