I am trying to run a 'batch' job of Firebase Firestore. Since batch job is asynchronous, and every batch processes only 500 documents, I have created an array of batch jobs that I want to run in a synchronous way, so that I know exactly when the last batch is done, and then proceed with the next operation.
However, to do this in Kotlin, when I read up, I have come across a sleuth of jargons like runBlocking, Coroutine, Dispatcher, async, await, Context, Suspend, launch, join, Scope, Deferred, Continuation, CommonPool
Furthermore, many of the posts say that in the latest version of Kotlin things have changed. Kotlin documentation talks about runBlocking, but this post says runBlocking is bad thing.
After some trial and error I got this to get compiled
suspend fun doTheThing() {
for ( b in batchArray) {
coroutineScope {
val job = async { b.commit() }
job.await()
}}
}
However, now I am getting the error saying "Suspend function 'doTheThing' should be called only from a coroutine or another suspend function" I am just confused at the moment. I just want to do those calls in sequence, or wait till all of those are done. Not sure what is the right syntax to get this done, and what concepts am I getting wrong.
Update: Following code snippet seems to be working:
for ( b in batchArray) {
runBlocking {b.commit()}
}
Is it a good practice to do it like this?