Call a API synchronously inside For Each loop in android Kotlin Coroutines with Flow getting Error
Call a API synchronously inside For-each loop in android Kotlin Coroutines using Flow.
Also i need to wait all the API calls need to complete without blocking the Main Thread.
fun getLatesData(): Flow<Model> = flow {
emit(getFirstList())
}.flatMapLatest { data ->
flow {
val responseList = mutableListOf<String>()
data.items?.forEach { datalist ->
val response = getCreatedDate(datalist.Id)
response.collect{count->
responseList.add(count.check)
}
val countData = Model(
datalist = data,
responseListCount = responseList
)
}
emit(countData)
}
}
private fun getCreatedDate(Id: String?)= flow {
try {
val response = repoApi.getCount(Id)
emit(response)
} catch (e: Exception) {
e.printStackTrace()
}
}.flowOn(Dispatchers.IO)
@Headers(
"Content-Type: application/json"
)
@GET("getCount/{Id}")
suspend fun getCount(@Path("Id")Id: String?): response
ERROR-kotlinx.coroutines.flow.internal.AbortFlowException: Flow was aborted, no more elements needed
For the first iteration Data is emitting properly, but second time onwards getting above mentioned error.
Run a API call without blocking the UI thread inside for loop with Flow. Thank in advance.
Comments
Post a Comment