Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9233583

Browse files
thatfiredevsamtstern
authored andcommitted
Demonstrate usage of FirebaseFirestore.runBatch() method (firebase#137)
1 parent e31f5fc commit 9233583

File tree

1 file changed

+11
-13
lines changed
  • firestore/app/src/main/java/com/google/example/firestore/kotlin

1 file changed

+11
-13
lines changed

firestore/app/src/main/java/com/google/example/firestore/kotlin/DocSnippets.kt

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -433,23 +433,21 @@ abstract class DocSnippets(val db: FirebaseFirestore) {
433433

434434
fun writeBatch() {
435435
// [START write_batch]
436-
// Get a new write batch
437-
val batch = db.batch()
438-
439-
// Set the value of 'NYC'
440436
val nycRef = db.collection("cities").document("NYC")
441-
batch.set(nycRef, City())
442-
443-
// Update the population of 'SF'
444437
val sfRef = db.collection("cities").document("SF")
445-
batch.update(sfRef, "population", 1000000L)
446-
447-
// Delete the city 'LA'
448438
val laRef = db.collection("cities").document("LA")
449-
batch.delete(laRef)
450439

451-
// Commit the batch
452-
batch.commit().addOnCompleteListener {
440+
// Get a new write batch and commit all write operations
441+
db.runBatch { batch ->
442+
// Set the value of 'NYC'
443+
batch.set(nycRef, City())
444+
445+
// Update the population of 'SF'
446+
batch.update(sfRef, "population", 1000000L)
447+
448+
// Delete the city 'LA'
449+
batch.delete(laRef)
450+
}.addOnCompleteListener {
453451
// ...
454452
}
455453
// [END write_batch]

0 commit comments

Comments
 (0)