0
fun viewDidLoad() {
    observeViewModel()
    
    initializeUI()
    getVideos()
    getQuestions()
}

fun initializeUI() {
    /* 
    1 - Create a list as;
    VideosTitleRow("Videos"),
    VideosBodyRow(null),
    QuestionTitleRow("Questions"),
    QuestionsBodyRow(null), 
    ...."
    
    Note: The bodies are null right now. There will fill with correct values after.
    
    ---
    
    2 - Call updateUI functions for update ui with initialize list.
    */
}

fun getVideos() {
    viewModel.getVideos()
}

fun getQuestions() {
    viewModel.getQuestions()
}

fun observeViewModel() {
    // Videos
    CoroutineScope(Dispatchers.IO).launch {
        when (flow) {
            // ...
            is VideosFlow.DataReceived -> {
                val row = SectionVideosBodyData(flow.videos)
                updateUI(1, row)
            }
        }
    }

    // Questions
    CoroutineScope(Dispatchers.IO).launch {
        when (flow) {
            // ...
            is QuestionsFlow.DataReceived -> {
                val row = SectionQuestionsBodyData(flow.questions)
                updateUI(3, row)
            }
        }
    }

    // And others LiveDatas under observing ....
}

fun updateUI(indexAt: Int? = null, row: BaseAdapterData = null) {
    /* 
    If indexAt and row parameters are not null; 
    edit the global row list then created new row list from this (global list) and send the new list to adapter for update ui.
    
    If parameters are null; send the global list to adapter for update ui.
    */
}

This is the current structure.

Let me try to explain what is problem;

When i start the initializeUI() function i know when it will finish the job. Because this is a synchronous function.

getVideos() or getQuestions() functions are a synchronous functions too but they start to api call, send request, take response, convert to ui model, bla bla operations. And we have to observe the results. So, we don't know when this job will finish!

getVideos() function just like says to viewModel.getVideos; "Let me know when you see the red car" but we can't know when is the red car pass on street.

In this case; although getQuestions() function is called before getVideos(), it may finish before it. In fact, both can end at the same time.

Here is i need;

I want to use kotlin coroutines scopes in getVideos(), getQuestions(), ... functions run as asynchronous but every job must wait for finished updateUI() function.

1- initializeUI() function runs, create a list, send to adapter, update the ui,

2- getVideos() or getQuestions() runs as asynchronous,

let say; livedata(getVideos) finish the their work but livedata (getQuestions) still working.

getQuestion() when call the updateUI() function, getVideos() can not call until updateUI() function finished called from getQuestion()

I know this is a complicated case :)

But the LiveData is messing up my mind.

I tried Mutex but although getVideos() or getQuestions() functions are synchronous, they starts an asynchronous job.

(Kotlin Coroutines sequential execution)

0 Answers0