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

时间:2018-10-06 08:45:46

标签: android firebase google-cloud-firestore

我想一次在FireStore android中为不同的文档推送数据。如何处理?

1 个答案:

答案 0 :(得分:1)

使用批量写入 issue

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) {
        // ...
    }
});