I created an app that has chat in it and I use firestore collection to store all the messages in it.
I gave an option to the users to delete their account which then means to delete that collection.
I saw that there is no straight forward way to delete collection and basically what I need to do is to run over all of the documents and delete them.
I saw that there is some solution given is this link that suggests using an Executor to perform the delete.
private void deleteCollection(final CollectionReference collection, Executor executor) {
Tasks.call(executor, () -> {
int batchSize = 10;
Query query = collection.orderBy(FieldPath.documentId()).limit(batchSize);
List<DocumentSnapshot> deleted = deleteQueryBatch(query);
while (deleted.size() >= batchSize) {
DocumentSnapshot last = deleted.get(deleted.size() - 1);
query = collection.orderBy(FieldPath.documentId()).startAfter(last.getId()).limit(batchSize);
deleted = deleteQueryBatch(query);
}
return null;
});
}
Can someone please explain how to use that executor?
I couldn't find a source that I could understand. Tried the following but seems like nothing was deleted from my database because I'm not sure if I execute correctly:
Executor executor = Runnable::run;
db.collection( "Chats" ).whereEqualTo( "ReceiverID", auth.getUid() ).get()
.addOnCompleteListener( task -> {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
if (document != null) {
CollectionReference cr = db.collection( "Chats" ).document( document.getId() ).collection( document.getId() );
deleteCollection(cr, executor);
}
}
}
} );
I also tried to use the Executor as follows:
executor = new Executor() {
@Override
public void execute(Runnable command) {
executor.execute( command );
}
};
But I got an error saying:
java.lang.StackOverflowError: stack size 8MB
So I'm pretty lost with how to call it.
Also, Is there any difference between using this solution to just using for loop and delete each document without using executor?
Thank you