title: Why does my Kotlin Flow onCompletion never run?
body: <p>So I have a flow where I need it to emit a value from cache, but at the end it will make an API call to pull values in case there was nothing in cache (or refresh the value it has). I am trying this</p>
  <pre><code>override val data: Flow&lt;List&lt;Data&gt;&gt; = dataDao.getAllCachedData()
      .onCompletion {
          coroutineScope {
              launch {
                  requestAndCacheDataOrEmitError()
              }
          }
      }
      .map { entities -&gt;
          entities
              .map { it.toData() }
              .filter { it !is Data.Unknown }
      }
      .filterNotNull()
      .catch { emitRepositoryError(it) }
  </code></pre>
  <p>So the idea is that we emit the cache, and then make an API call to fetch new data regardless of the original mapping. But I do not want it blocking. For example, if we use this flow, I do not ever want the calling function to be blocked by the onCompletion.</p>
  <p>I think the problem is that the <code>onCompletion</code> never runs. I set some breakpoints/logs and it never runs at all, even outside of the <code>coroutineScope</code>.</p>