title: Wait until Kotlin coroutine finishes in onCreateView()
body: <p>I have an initialization block in <code>onCreateView</code>, where some variables are assigned from SharedPreferences, DB or Network (currently from SharedPreferences).</p>
  
  <p>I want to update views with these values in <code>onViewCreated</code>. But they update with empty values before a coroutine in <code>onCreateView</code> finishes. How to wait until the coroutine finishes in main thread?</p>
  
  <pre><code>override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
      ...
      GlobalScope.launch(Dispatchers.Main) {
          val job = GlobalScope.launch(Dispatchers.IO) {
              val task = async(Dispatchers.IO) {
                  settingsInteractor.getStationSearchCountry().let {
                      countryName = it.name
                  }
                  settingsInteractor.getStationSearchRegion().let {
                      regionName = it.name
                  }
              }
              task.await()
          }
          job.join()
      }
  }
  
  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
      super.onViewCreated(view, savedInstanceState)
  
      country.updateCaption(countryName)
      region.updateCaption(regionName)
  }
  </code></pre>
  
  <p>UPDATE (20-05-2019)</p>
  
  <p>Currently I don't use <code>onViewCreated</code>. I write code in <code>onCreate</code> and <code>onCreateView</code>. In <code>onCreateView</code> I access views this way: <code>view.country.text = "abc"</code> and so on.</p>