如何在FiRESTORE中计算集合下的文档数?我想让收藏参考在CloudFiRESTORE上存在的计数,我尝试使用:FirebaseFirestore db = FirebaseFirestore.getInstance();
final CollectionReference postsCollection = db.collection("Posts");
final TaskCompletionSource<Integer> source = new TaskCompletionSource<>();
new Thread(new Runnable() {
@Override
public void run() {
int fromWhereToStart = postsCollection.get().getResult().size();
source.setResult(fromWhereToStart);
}
}).start();
Task<Integer> task = source.getTask();
task.addOnCompleteListener(new OnCompleteListener<Integer>() {
@Override
public void onComplete(@NonNull Task<Integer> task) {
Log.e("Z_fromWhereToStart", "= " + task.getResult());
}
});但不幸的是,我得到了:java.lang.IllegalStateException: Task is not yet complete是否有另一种方法来获取另一种方法的计数来修复IllegalStateException?
2 回答
一只甜甜圈
TA贡献1836条经验 获得超5个赞
if (task.isSuccessful())
for (DocumentSnapshot document : task.getResult()){ yourCounter++;}
counter
"Posts"
添加回答
举报
0/150
提交
取消