If you have used Firebase database, writing to completely single separate locations atomically was not possible, that's why you would have to use batch writes, which means that either all of the operations succeed, or none of them are applied.
Regarding Firestore, all operations are now atomically processed. However, you can execute multiple write operations as a single batch that contains any combination of set(), update(), or delete() operations. A batch of writes completes atomically and can write to multiple documents.
This a simple example regarding a batch operation for write, update and delete operation.
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 age
DocumentReference alexRef = db.collection("users").document("Alex");
batch.delete(alexRef);
batch.commit().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
// ...
}
});
Calling commit()
method on the batch object means that you commit the entire batch.