in firestore/testapp/Assets/Firebase/Sample/Firestore/UIHandler.cs [305:397]
private IEnumerator PerformBatchWrite() {
DocumentReference doc1 = db.Collection("col2").Document("batch_doc1");
DocumentReference doc2 = db.Collection("col2").Document("batch_doc2");
DocumentReference doc3 = db.Collection("col2").Document("batch_doc3");
// Initialize doc1 and doc2 with some data.
var initialData = new Dictionary<string, object>{
{"field", "value"},
};
yield return new WaitForTaskCompletion(this, doc1.SetAsync(initialData));
yield return new WaitForTaskCompletion(this, doc2.SetAsync(initialData));
// Perform batch that deletes doc1, updates doc2, and overwrites doc3.
DebugLog("INFO: Going to perform the following three operations in a batch:");
DebugLog("\tDelete col2/batch_doc1");
DebugLog("\tUpdate col2/batch_doc2");
DebugLog("\tOverwrite col2/batch_doc3");
yield return new WaitForTaskCompletion(this, doc1.Firestore.StartBatch()
.Delete(doc1)
.Update(doc2, new Dictionary<string, object> { { "field2", "value2" } })
.Update(doc2, new Dictionary<FieldPath, object> { { new FieldPath("field3"), "value3" } })
.Update(doc2, "field4", "value4")
.Set(doc3, initialData)
.CommitAsync());
DebugLog("INFO: Batch operation completed.");
DebugLog("INFO: Checking the resulting documents.");
Task<DocumentSnapshot> get1 = doc1.GetSnapshotAsync();
yield return new WaitForTaskCompletion(this, get1);
if (get1.IsCanceled) {
DebugLog("INFO: Read operation for batch_doc1 was cancelled.");
} else if (get1.IsFaulted) {
DebugLog("ERROR: An error occurred while retrieving col2/batch_doc1.");
DebugLog("ERROR: " + get1.Exception.ToString());
} else {
DocumentSnapshot snap = get1.Result;
if(snap.Exists) {
DebugLog("ERROR: col2/batch_doc1 should have been deleted.");
} else {
DebugLog("Success: col2/batch_doc1 does not exist.");
}
}
Task<DocumentSnapshot> get2 = doc2.GetSnapshotAsync();
yield return new WaitForTaskCompletion(this, get2);
if (get2.IsCanceled) {
DebugLog("INFO: Read operation for batch_doc2 was cancelled.");
} else if (get2.IsFaulted) {
DebugLog("ERROR: An error occurred while retrieving col2/batch_doc2.");
DebugLog("ERROR: " + get2.Exception.ToString());
} else {
DocumentSnapshot snap = get2.Result;
if(snap.Exists) {
bool deepEquals = ObjectDeepEquals(snap.ToDictionary(),
new Dictionary<string, object> {
{ "field", "value" },
{ "field2", "value2" },
{ "field3", "value3" },
{ "field4", "value4" },
});
if(deepEquals) {
DebugLog("Success: col2/batch_doc2 content is as expected.");
} else {
DebugLog("ERROR: col2/batch_doc2 has incorrect content.");
}
} else {
DebugLog("ERROR: col2/batch_doc2 does not exist.");
}
}
Task<DocumentSnapshot> get3 = doc3.GetSnapshotAsync();
yield return new WaitForTaskCompletion(this, get3);
if (get3.IsCanceled) {
DebugLog("INFO: Read operation for batch_doc3 was cancelled.");
} else if (get3.IsFaulted) {
DebugLog("ERROR: An error occurred while retrieving col2/batch_doc3.");
DebugLog("ERROR: " + get3.Exception.ToString());
} else {
DocumentSnapshot snap = get3.Result;
if(snap.Exists) {
bool deepEquals = ObjectDeepEquals(snap.ToDictionary(), initialData);
if(deepEquals) {
DebugLog("Success: col2/batch_doc3 content is as expected.");
} else {
DebugLog("ERROR: col2/batch_doc3 has incorrect content.");
}
} else {
DebugLog("ERROR: col2/batch_doc3 does not exist.");
}
}
}