Skip to content

Instantly share code, notes, and snippets.

@alan89
Created August 13, 2019 00:54
Show Gist options
  • Save alan89/e3aa2a4ec06f62e6705c4d4f57042aa1 to your computer and use it in GitHub Desktop.
Save alan89/e3aa2a4ec06f62e6705c4d4f57042aa1 to your computer and use it in GitHub Desktop.
// This example just includes two functions to illustrate how to split write operations
// in a series of WriteBatch ones, in order to avoid the 500 writes limit per execution
// https://firebase.google.com/docs/firestore/manage-data/transactions#batched-writes
// splitWriteBatch creates the document references to set the values and write the
// data in a series of WriteBatch operations.
// In this example, we write 650 documents, divided in 2 chunks
public void splitWriteBatch() {
// First we need to work with the data to write.
// We created the reference for those documents
ArrayList<DocumentReference> refArray = new ArrayList<>();
for (int i = 0; i < 650; i++) {
refArray.add(mFirestore.collection("cities").document(String.valueOf(i)));
}
// Then we prepare the operations using an array of batches and the current Index
ArrayList<WriteBatch> batches = new ArrayList<>();
int currentBatch = 0;
// We will write the same object in all the document references, just for the example
Map<String, Object> city = new HashMap<>();
city.put("name", "San Francisco");
// Going through the array and split the content in small chunks
for (int j=0 ; j < refArray.size(); j++) {
// If is the first operation, the index remains as 0
// We create the first WriteBatch reference
if (j == 0) {
batches.add(mFirestore.batch());
Log.d(TAG, "batch: index " + currentBatch);
}
// Check if we have more than 500 to create a new WriteBatch
// and increment the index
else if (j % 499 == 0){
batches.add(mFirestore.batch());
currentBatch ++;
Log.d(TAG, "batch: index " + currentBatch);
}
// The write operations are added to the current batch operation
batches.get(currentBatch).set(refArray.get(j), city);
}
// Once we split the data in small chunks (with less than 500 elements)
// the batch operations are triggered.
executeBatch (batches,0);
}
// ExecuteBatch is intended to execute the WriteBatch operations in a synchronous way
// waiting until the previous execution is finished
public void executeBatch (final ArrayList<WriteBatch> batches, final int index) {
// Commit the current batch
batches.get(index).commit().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
// Once the first WriteBatch operation is finished, we increment
// the index and compare it with the batches size
int tempIndex = index + 1;
if ( tempIndex < batches.size() ){
// if is minor than the size, we trigger the next batch
executeBatch(batches, tempIndex);
}
else {
// If there are no more batch, we will show a toast.
Toast toast = Toast.makeText(getApplicationContext(),
"Write batch concluded",
Toast.LENGTH_SHORT);
toast.show();
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment