2

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

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)
4

1 回答 1

13

哎呀,那些仿制药在里面受伤了。你会想要使用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);

                    }
                }
            }
        });
于 2017-12-29T20:07:53.117 回答