0

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

Ben
  • 1,737
  • 2
  • 30
  • 61
  • 1
    When an Executor is needed is because an action may (potentially) take long to complete and could cause a ANR (application not responding), hence it needs to be run off the main thread. It is good practice, and highly recommended nearing the edge of mandatory, to perform any type of Network / IO / Database operations, in a separate thread to avoid locking the main thread, hence the need of an Executor. – PerracoLabs Oct 22 '20 at 17:58
  • Thank you for the explanation. Now I only need to understand how to actually use this executor =] – Ben Oct 22 '20 at 20:05
  • 1
    I found this [Android reference](https://developer.android.com/reference/java/util/concurrent/Executor) and some [Stackoverflow posts](https://stackoverflow.com/questions/48358608/which-are-the-recomended-arguments-to-use-when-deleting-a-collection-within-a-cl) that maybe could help you to better understand – Andie Vanille Oct 22 '20 at 21:36

0 Answers0