Can I use async{} inside kotlin flow .
Scenario: After the API call I get a list of 200 objects that I need to parse (convert to UIObject). I am trying to process this list in parallel. Below is the Pseudo code:
fun getUIObjectListFlow():Flow<List<UIObject>> {
flow<List<UIObject>> {
while (stream.hasNext()) {
val data = stream.getData() // reading from an input stream. Data comes in chunk
val firstHalfDeffered = async(Dispatchers.IO) { /* process first half of the list that data obj contains*/ }
val secondHalfDeffered = async(Dispatchers.IO) { /*process second half of the list that data obj contains */ }
val processedList = firstHalfDeffered.await() + secondHalfDeffered.await() // just pseudo code
emit(processedList)
}
}
}
As async{} requires Coroutine scope (eg: someScope.async{} ), how can I get a scope inside flow ? Is there any other approach to accomplish this ?
This function is in the repository and I am calling it from viewmodel.
Thanks