Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在FireStore Android中一次更新和添加多个文档的数据?_Android_Firebase_Google Cloud Firestore - Fatal编程技术网

如何在FireStore Android中一次更新和添加多个文档的数据?

如何在FireStore Android中一次更新和添加多个文档的数据?,android,firebase,google-cloud-firestore,Android,Firebase,Google Cloud Firestore,我希望在FireStore android中一次推送不同文档的数据。如何处理此问题?使用批写入 我想在一个集合中更新多个文档。然后我想检查文档是否存在。@Balasndark在OnCompleteListener中使用task.isSuccessful。只有当所有写入都成功时,它才会返回true。如果其中一次写入失败,整个批处理将被取消。我在For循环中使用单个DocumentReference,我需要检查文档是否存在,然后更新文档。。什么是解决方案?@Balasandark您不应该循环查询,

我希望在FireStore android中一次推送不同文档的数据。如何处理此问题?

使用批写入


我想在一个集合中更新多个文档。然后我想检查文档是否存在。@Balasndark在OnCompleteListener中使用task.isSuccessful。只有当所有写入都成功时,它才会返回true。如果其中一次写入失败,整个批处理将被取消。我在For循环中使用单个DocumentReference,我需要检查文档是否存在,然后更新文档。。什么是解决方案?@Balasandark您不应该循环查询,因为它们正在后台线程上工作。使用完整的监听器。最后,我解决了我的问题,如果失败,我会在我的引用中更新查询。我会将集合查询添加到失败的监听器中。谢谢@GoCrafter\u LP
WriteBatch batch = db.batch();

// Set the value of 'NYC'
DocumentReference nycRef = db.collection("cities").document("NYC");
batch.set(nycRef, new City());

// Update the population of 'SF'
DocumentReference sfRef = db.collection("cities").document("SF");
batch.update(sfRef, "population", 1000000L);

// Delete the city 'LA'
DocumentReference laRef = db.collection("cities").document("LA");
batch.delete(laRef);

// Commit the batch
batch.commit().addOnCompleteListener(new OnCompleteListener<Void>() {
    @Override
    public void onComplete(@NonNull Task<Void> task) {
        // ...
    }
});