That is what i'm trying to do; first showing items with loading progress in my ui, when is the api request returned as success then i'm manipulating my list for pass to recyclerview adapter.
So i need call repeatedly updateAndSetAdapterList function in coroutines.
Here is my code that calling same function in Fragment;
lifecycleScope.launch(Dispatchers.IO) {
// Videos
viewModel.dashboardVideosFlow.collectLatest { flow ->
when (flow) {
...
is VideosFlow.DataReceived -> {
val row = SectionVideosBodyData(flow.videos)
updateAndSetAdapterList(1, row) <----
}
}
}
}
lifecycleScope.launch(Dispatchers.IO) {
// Questions
viewModel.dashboardQuestionsFlow.collectLatest { flow ->
when (flow) {
....
is QuestionsFlow.DataReceived -> {
val row = SectionQuestionsBodyData(flow.questions)
updateAndSetAdapterList(3, row) <----
}
}
}
}
And this is the function that does something first on background thread after on main thread;
private suspend fun updateAndSetAdapterList(indexAt: Int? = null, row: BaseAdapterData? = null) {
lifecycleScope.launch(Dispatchers.IO) {
val newRows = mutableListOf<BaseAdapterData>()
newRows.addAll(rows)
if (indexAt != null && row != null) {
newRows.removeAt(indexAt)
newRows.add(indexAt, row)
}
withContext(Dispatchers.Main) {
dashboardAdapter.submitList(newRows)
}
rows = newRows
}
}
I want to use synchronous api call functions but i want to use the ui update function as asynchronous that called from these synchronous functions.