title: How to efficiently show loading dialog if kotlin coroutine job takes quite some time to complete?
body: <p>What I want to do is to use kotlin coroutines for database operations and show users a loading screen in the meantime. My basic implementation is as follows:</p>
  
  <pre><code>fun loadSomeData(){
      mainCoroutineScope.launch { 
          showLoadingDialog()
          // suspening function over Dispatchers.IO, returns list
          val dataList = fetchDataFromDatabase()
          inflateDataIntoViews(dataList)
          hideLoadingDialog()
      }
  }
  </code></pre>
  
  <p>This works perfectly for me when the loading takes quite some time for large datasets. But in scenarios where the <code>fetchDataFromDatabase()</code> finishes quickly, showing and hiding the dialog box in quick succession creates an annoying glitching effect.</p>
  
  <p>So what I want is to show the dialog box only if the <code>fetchDataFromDatabase()</code> function take more than, <em>lets say</em>, 100 ms to complete.</p>
  
  <p>So my question is, <strong>what is the performance efficient way to achieve this using kotlin coroutines?</strong></p>