如果您使用过Firebase数据库,则不可能原子地完全写入单个单独的位置,这就是为什么您必须使用批量写入的原因,这意味着要么所有操作成功,要么不应用任何操作。
关于Firestore,所有操作现在都经过原子处理。但是,您可以作为一个批处理执行多个写操作,其中包含set(),update()或delete()操作的任意组合。一批写操作可以自动完成,并且可以写到多个文档。
这是一个关于批处理操作的简单示例,该批处理用于写入,更新和删除操作。
WriteBatch batch = db.batch();documentReference johnRef = db.collection("users").document("John");batch.set(johnRef, new User());documentReference maryRef = db.collection("users").document("Mary");batch.update(maryRef, "Anna", 20); //Update name and agedocumentReference alexRef = db.collection("users").document("Alex");batch.delete(alexRef);batch.commit().addonCompleteListener(new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> task) { // ... }});commit()批处理对象上的调用方法意味着您提交了整个批处理。



