void AddDataBatchedWrites()

in firestore/android/FirestoreSnippetsCpp/app/src/main/cpp/snippets.cpp [301:339]


void AddDataBatchedWrites(firebase::firestore::Firestore* db) {
  using firebase::Future;
  using firebase::firestore::DocumentReference;
  using firebase::firestore::Error;
  using firebase::firestore::FieldValue;
  using firebase::firestore::WriteBatch;

  // If you do not need to read any documents in your operation set, 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. The following
  // example shows how to build and commit a write batch:

  // [START write_batch]
  // Get a new write batch
  WriteBatch batch = db->batch();

  // Set the value of 'NYC'
  DocumentReference nyc_ref = db->Collection("cities").Document("NYC");
  batch.Set(nyc_ref, {});

  // Update the population of 'SF'
  DocumentReference sf_ref = db->Collection("cities").Document("SF");
  batch.Update(sf_ref, {{"population", FieldValue::Integer(1000000)}});

  // Delete the city 'LA'
  DocumentReference la_ref = db->Collection("cities").Document("LA");
  batch.Delete(la_ref);

  // Commit the batch
  batch.Commit().OnCompletion([](const Future<void>& future) {
    if (future.error() == Error::kErrorOk) {
      std::cout << "Write batch success!" << std::endl;
    } else {
      std::cout << "Write batch failure: " << future.error_message() << std::endl;
    }
  });
  // [END write_batch]
}