运行多个Firestore查询并等待所有查询完成 [英] Run multiple Firestore queries and wait for all of them to be completed

查看:51
本文介绍了运行多个Firestore查询并等待所有查询完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我想在开始片段之前对数据库执行4个查询.

In my application I would like to perform 4 queries to my database before starting a fragment.

我试图实现的目标是:

Task t1 = Query1.get().onSuccesListener.... ; 
Task t2 = Query2.get().onSuccesListener.... ; 
Task t3 = Query3.get().onSuccesListener.... ; 
Task t4 = Query4.get().onSuccesListener.... ; 

无论如何,我都找不到在Android中使用 Task 进行操作的方法,因此我尝试在 AsynTask 内部运行查询,然后等待所有 AsyncTask onPostExecuted 完成.

I couldnt find anyway to do them with Task in Android so I tried to run a query inside of a AsynTaskand then wait for all AsyncTask to be done by using a Latch onPostExecuted.

以下是我的 AsyncTask 之一与查询的示例.

Here an example of one of my AsyncTask with the query.

private class GetLandlordsRoomQuery extends AsyncTask<Void, Void, Void> {
        ArrayList<RoomPosted> landlordsRooms = new ArrayList<>();
        @Override
        protected Void doInBackground(Void... voids) {
            Query removeNonTenant = FirebaseFirestore.getInstance()
                    .collection(DataBasePath.ROOMS.getValue())
                    .whereEqualTo("landlordID", FirebaseAuth.getInstance().getUid());

            removeNonTenant.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                @Override
                public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                    for (DocumentSnapshot d : queryDocumentSnapshots) {
                        landlordsRooms.add(d.toObject(RoomPosted.class));
                    }
                }
            });
            return null;
        }
        @Override
        public void onPostExecute(Void result) {
            mLandlordsRooms = landlordsRooms;
            if (--asyncTaskLatch == 0){
                startFragmentFromLandlord();
            }
        }
    }

我在这里运行的问题是onPostExecuted在查询完成之前运行.

The problem that I run in here is that the onPostExecuted runs before the query is completed.

我可能使用AsyncTask解决此问题的方法有误,但是找不到使用 Task 的方法来等待所有4个任务完成而又没有与它们嵌套嵌套的onCompleteListener.

I might have the wrong approach to solve this issue with AsyncTask but couldnt find a way using Task to wait for all 4 task to be completed without making a nested onCompleteListener with them.

有什么想法可以在所有4个线程中并行运行并等待它们完成后再加载frament吗?

Any ideas how I could run in paralell all 4 threads and wait for them to be completed before I load my frament?

推荐答案

您可以使用Tasks的

You can simply merge all your 4 separate queries locally, using Tasks's whenAllSuccess() method. You can achieve this, using the following lines of code:

Task t1 = Query1.get();
Task t2 = Query2.get();
Task t3 = Query3.get();
Task t4 = Query4.get();

Task combinedTask = Tasks.whenAllSuccess(t1, t2, t3, t4).addOnSuccessListener(new OnSuccessListener<List<Object>>() {
    @Override
    public void onSuccess(List<Object> list) {
         //Do what you need to do with your list
    }
});

如您所见,当覆盖 onSuccess()方法时,结果是从这些查询中获得的对象的 list .

As you can see, when overriding the onSuccess() method the result is a list of objects that you get from those queries.

Cloud Firestore客户端已经在后台线程中运行所有网络操作.这意味着所有操作都将在不阻塞主线程的情况下进行.将其放在 AsyncTask 中不会带来任何其他好处.

The Cloud Firestore client, already runs all network operations in a background thread. This means that all operations take place without blocking your main thread. Putting it in an AsyncTask does not give any additional benefits.

这篇关于运行多个Firestore查询并等待所有查询完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆