gpt4 book ai didi

android - Firestore 任务 whenallcomplete() - 任务尚未完成

转载 作者:行者123 更新时间:2023-11-29 14:21:10 25 4
gpt4 key购买 nike

第一次使用适用于 Android 的 Google Tasks API 并遇到了这个问题:使用 Tasks.whenAllComplete 或 .whenAllSuccess() 和 OnCompleteListener 或 OnSuccessListener 即使我检查了 task.isComplete,我仍收到错误 Task is not yet complete () 并返回真值。知道这里出了什么问题吗?

db.collection("calendar").get()
.continueWith(new Continuation<Task<QuerySnapshot>, Task<?>>() {
@Override
public Task<?> then(@NonNull Task<Task<QuerySnapshot>> task) throws Exception {
List<Task<QuerySnapshot>> tasks = new ArrayList<Task<QuerySnapshot>>();
for (DocumentSnapshot ds : task.getResult().getResult())
tasks.add(ds.getReference().collection("thingstodo").get());

return Tasks.whenAllSuccess(tasks);
}
})
.addOnCompleteListener(new OnCompleteListener<Task<?>>() {
@Override
public void onComplete(@NonNull Task<Task<?>> task) {
List<QuerySnapshot> lists = (ArrayList<QuerySnapshot>)task.getResult().getResult(); //error in this line
for (QuerySnapshot qs : lists)
for (DocumentSnapshot ds: qs) {
ScheduledItem item = ds.toObject(ScheduledItem.class);
//add to list including day
itemsList.add(item);

}

//list ready to be used!
}
});

这是错误:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.tripjoe.tripjoe, PID: 6161
java.lang.IllegalStateException: Task is not yet complete
at com.google.android.gms.common.internal.zzbq.zza(Unknown Source:8)
at com.google.android.gms.tasks.zzn.zzbjk(Unknown Source:5)
at com.google.android.gms.tasks.zzn.getResult(Unknown Source:3)
at com.tripjoe.tripjoe.services.Wizard$4.onSuccess(Wizard.java:74)
at com.tripjoe.tripjoe.services.Wizard$4.onSuccess(Wizard.java:71)
at com.google.android.gms.tasks.zzj.run(Unknown Source:27)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

最佳答案

哎呀,那些泛型伤到内心了😄。你会想要使用 continueWithTask .这是使用更好的泛型和 continueWithTask 修复的代码:

FirebaseFirestore.getInstance().collection("calendar").get()
.continueWithTask(new Continuation<QuerySnapshot, Task<List<QuerySnapshot>>>() {
@Override
public Task<List<QuerySnapshot>> then(@NonNull Task<QuerySnapshot> task) {
List<Task<QuerySnapshot>> tasks = new ArrayList<Task<QuerySnapshot>>();
for (DocumentSnapshot ds : task.getResult()) {
tasks.add(ds.getReference().collection("thingstodo").get());
}

return Tasks.whenAllSuccess(tasks);
}
})
.addOnCompleteListener(new OnCompleteListener<List<QuerySnapshot>>() {
@Override
public void onComplete(@NonNull Task<List<QuerySnapshot>> task) {
// BTW, `getResult()` will throw an exception if the task fails unless you first check for `task.isSuccessful()`
List<QuerySnapshot> list = task.getResult();
for (QuerySnapshot qs : list) {
for (DocumentSnapshot ds : qs) {
ScheduledItem item = ds.toObject(ScheduledItem.class);
//add to list including day
itemsList.add(item);

}
}
}
});

关于android - Firestore 任务 whenallcomplete() - 任务尚未完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48027696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com