Search in sources :

Example 6 with WriteBatch

use of com.google.firebase.firestore.WriteBatch in project snippets-android by firebase.

the class DocSnippets method deleteQueryBatch.

/**
 * Delete all results from a query in a single WriteBatch. Must be run on a worker thread
 * to avoid blocking/crashing the main thread.
 */
@WorkerThread
private List<DocumentSnapshot> deleteQueryBatch(final Query query) throws Exception {
    QuerySnapshot querySnapshot = Tasks.await(query.get());
    WriteBatch batch = query.getFirestore().batch();
    for (QueryDocumentSnapshot snapshot : querySnapshot) {
        batch.delete(snapshot.getReference());
    }
    Tasks.await(batch.commit());
    return querySnapshot.getDocuments();
}
Also used : QueryDocumentSnapshot(com.google.firebase.firestore.QueryDocumentSnapshot) WriteBatch(com.google.firebase.firestore.WriteBatch) QuerySnapshot(com.google.firebase.firestore.QuerySnapshot) WorkerThread(androidx.annotation.WorkerThread)

Aggregations

WriteBatch (com.google.firebase.firestore.WriteBatch)6 DocumentReference (com.google.firebase.firestore.DocumentReference)4 CollectionReference (com.google.firebase.firestore.CollectionReference)2 MenuItem (android.view.MenuItem)1 NonNull (androidx.annotation.NonNull)1 WorkerThread (androidx.annotation.WorkerThread)1 Rating (com.google.firebase.example.fireeats.java.model.Rating)1 Restaurant (com.google.firebase.example.fireeats.java.model.Restaurant)1 QueryDocumentSnapshot (com.google.firebase.firestore.QueryDocumentSnapshot)1 QuerySnapshot (com.google.firebase.firestore.QuerySnapshot)1