【发布时间】:2018-08-29 10:16:28
【问题描述】:
我正在使用 Firestore 分片来跟踪集合中的文档数量。如何将它返回的任务转换为字符串?
我的方法(与 Firebase 文档相同):
public Task<Integer> getCount(final DocumentReference ref) {
// Sum the count of each shard in the subcollection
return ref.collection("shards").get()
.continueWith(new Continuation<QuerySnapshot, Integer>() {
@Override
public Integer then(@NonNull Task<QuerySnapshot> task) throws Exception {
int count = 0;
for (DocumentSnapshot snap : task.getResult()) {
Shard shard = snap.toObject(Shard.class);
count += shard.count;
}
Log.d(TAG, Integer.toString(count));
return count;
}
});
}
正确记录计数如下:D/saveMessageSent: 1
但是,当我像这样在其他地方调用 getCount 方法时:
Log.d(TAG, String.valueOf(getCount(counterDocRef)));
那么日志输出为:
D/saveMessageSent: com.google.android.gms.tasks.zzu@8673e29
我似乎无法将它转换为字符串,因为它说它是一个
com.google.android.gms.tasks.Task<java.lang.Integer>
我该如何解决这个问题,所以当我调用 getCount 时,它会给我一个我可以使用的 int?
【问题讨论】:
标签: android firebase task google-cloud-firestore