在Android中循环运行Firebase的get()函数。

huangapple go评论65阅读模式
英文:

Running Firebase get() in a loop in android

问题

我正在处理一个安卓项目,在使用循环获取 Firebase Firestore 文档时遇到了问题。

// 从 uidS 中获取文档快照
for (int i = 0; i < uidS.size(); i++) {
    db.collection("items")
            .document(uidS.get(i))
            .get()
            .addOnCompleteListener(task1 -> {

                if (task1.isSuccessful()) {
                    documentSnapshots.add(task1.getResult());

                    if (documentSnapshots.size() == uidS.size()) {
                        // 在这里进行操作...
                    }

                } else {
                    Toast.makeText(this, "错误!", Toast.LENGTH_SHORT).show();
                }
            });
}

> 在这里,我有一个 uidS 数组列表,其中包含了从 Firestore 获取文档快照的所有 ID,然后将它们添加到 documentSnapshots 数组列表中。

现在的问题是,在 Firestore 任务 开始之前,循环就已经结束了,这使得循环中的索引在 Task 中的顺序变得有些随机,如下所示:

循环索引:0
循环索引:1
循环索引:2
循环索引:3
超出循环...
在具有索引 0 的 Firebase 任务中
在具有索引 2 的 Firebase 任务中
在具有索引 1 的 Firebase 任务中
在具有索引 3 的 Firebase 任务中

我知道这是 Firestore 的通常异步行为,但我没有其他方法来构建数据库或执行其他操作。我如何才能实现这一点?

> 虽然我知道阻塞主线程可能会导致 ANR,但我已经在我的代码中检查了网络连接速度,所以所有的文档快照都不会超过 2 秒。

是否有任何方法可以先执行任务,然后再执行 超出循环... 部分,或者有没有办法使 Task 中的索引顺序保持有序而不是随机的?

非常感谢您的任何帮助 在Android中循环运行Firebase的get()函数。

英文:

I am working on an android project and have encountered a problem in getting firebase firestore documents in a loop.

//getting the document snapshot out of the uIds
                        for (int i = 0; i &lt; uidS.size(); i++) {

                            db.collection(&quot;items&quot;)
                                    .document(uidS.get(i))
                                    .get()
                                    .addOnCompleteListener(task1 -&gt; {

                                        if (task1.isSuccessful()) {
                                            documentSnapshots.add(task1.getResult());

                                            if (documentSnapshots.size() == uidS.size()) {
                                                //Do something here...
                                            }

                                        } else {
                                            Toast.makeText(this, &quot;Error!!&quot;, Toast.LENGTH_SHORT).show();
                                        }
                                    });
                        }  

> Here I have a uidS arraylist which contains all the id for getting the documentSnapshot from Firestore which then are added to a documentSnapshots arraylist.

Now the problem is that the loop finishes off before the firestore task is even started which makes the order of the loop's index in Task be kind of random, like:

Loop Index:0
Loop Index:1
Loop Index:2
Loop Index:3
Beyond the loop....
In firebase task with index 0 
In firebase task with index 2 
In firebase task with index 1  
In firebase task with index 3  

I know it is the usual asynchronous behaviour of Firestore but I do not have any other way to structure the database or do anything else. How can i achieve this?

> Although, I know blocking the main thread may result in ANR but I have already checked for the internet connection speed in my code so all the documentSnapshots would take less than 2 seconds.

Is there any way I can make the task be executed first then beyond the loop... or any way to just make the order of index in Task be in order and not random?

Any help would be so much appreciated 在Android中循环运行Firebase的get()函数。

答案1

得分: 0

好的,针对具有相同情况的人,一个解决方案是使用另一个数据结构,该数据结构不依赖于元素添加的顺序。

> 我使用了一个 Hashmap<String,DocumentSnapshot> 来完成这项工作,其中字符串是UID

因此,无论以什么顺序添加它们,我们都可以通过它们各自的 UID 获取 DocumentSnapshot,进一步处理。

英文:

Okay so a solution for people, having the same kind of situation; would be using another data structure which does not depends upon the order of adding of elements.

> I used a Hashmap<String, DocumentSnapshot> to do the job with the string being the UID.

So, in whatever order they are added, we get the DocumentSnaphot by their respective UID, further.

huangapple
  • 本文由 发表于 2020年9月22日 00:01:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63995951.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定