title: Kotlin Coroutines: Issue with job-scheduling.(invokeOnCompletion)
body: <p>I am fairly new to this kotlin-coroutine thing and i have an issue with job-scheduling.In this code below, first i fetch topic names from user's cache in the fragment.(topicsList)
  And then, i need to fetch these topics from API one by one. What i want to do is loop through the topicsList, make a request for each topic and get all the responses once at the completion of all requests. In order to achieve that, in getEverything() method(which fires up a request), i am adding the responses into an arraylist for every time.(responseList)
  In for loop, i am firing up all the requests. After the completion of the job, job.invokeOnCompletion{} is called and i set my liveData to responseList. However, this approach doesn't work. Problem is, i am updating the liveData before the setting the responseList. I don't know how can it be possible. Could anybody help me about this?</p>
  <p>Here is my CoroutineScope in myFragment:</p>
  <pre><code>val topicsList = dataMap[&quot;topics&quot;] // GOT THE TOPICS
  topicsList?.let {
      var job: Job
      CoroutineScope(Dispatchers.Main).launch {
          job = launch {
              for (topic in topicsList) {
                  mViewModel.getEverything(topic, API_KEY)
              }
          }
          job.join()
          job.invokeOnCompletion {
          mViewModel.updateLiveData()
          }
      }
  } ?: throw Exception(&quot;NULL&quot;)
  </code></pre>
  <p>getEverything() method in viewModel:</p>
  <pre><code> suspend fun getEverything(topic: String, apiKey: String) {
  
          viewModelScope.launch {
              _isLoading.value = true
              withContext(Dispatchers.IO) {
                  val response = api.getEverything(topic, apiKey)
                  withContext(Dispatchers.Main) {
                      if (response.isSuccessful) {
                          if (response.body() != null) {
                              responseList.add(response.body()!!)
                              println(&quot;Response is successful: ${response.body()!!}&quot;)
                              _isLoading.value = false
                              _isError.value = false
                          }
                      }
                      else {
                          Log.d(TAG, &quot;getEverything: ${response.errorBody()}&quot;)
                          _isError.value = true
                          _isLoading.value = false
                      }
                  }
              }
          }
  
      }
  </code></pre>
  <p>And, updateLiveData method:</p>
  <pre><code>fun updateLiveData() {
          _newsResponseList.value = responseList
          println(&quot;response list : ${responseList.size}&quot;)
          responseList.clear()
  }
  
  </code></pre>
  <p>And this is how it looks in the logs: <a href="https://i.stack.imgur.com/IUBrV.png" rel="nofollow noreferrer">Logs</a></p>
  <p>Logs for you who cannot open the image :</p>
  <pre><code>I/System.out: response list : 0
  I/System.out: Response is successful: NewsResponse(articleList=[Article(source=Source(id=wired, ...
  I/System.out: Response is successful: NewsResponse(articleList=[Article(source=Source(id=techcrunch, ...
  I/System.out: Response is successful: NewsResponse(articleList=[Article(source=Source(id=wired, ...
  I/System.out: Response is successful: NewsResponse(articleList=[Article(source=Source(id=the-verge, ...
  </code></pre>
  <p>Btw data is fetched without an error and its correct. I've no issue with that.</p>