title: Wait for two LiveData to update before emitting flow
body: <p>I have two liveData <code>liveDataA</code> and <code>liveDataB</code> in <code>MyViewModel</code>.</p>
  <p>When a button is pressed both liveData increment by one via <code>incrementLiveData()</code>.</p>
  <p>I collect the combined flows with <code>collectLatest()</code> in <code>MainActivity</code>.</p>
  <p><strong>Expected outcome:</strong> Log prints once <code>&gt;&gt; 1 1</code> as <code>collectLatest()</code> will wait for both LiveData to update before emitting.</p>
  <p><strong>Actual outcome:</strong> Log prints twice <code>&gt;&gt; 1 0</code> and then <code>&gt;&gt; 1 1</code> i.e. it is not waiting for both to update before emitting.</p>
  <p>How can I have a flow that only emits once both <code>liveDataA</code> and <code>liveDataB</code> have been updated?</p>
  <blockquote>
  <p>MainActivity</p>
  </blockquote>
  <pre><code>class MainActivity : AppCompatActivity() {
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)
  
          val viewModel = MyViewModel()
          val button = findViewById&lt;Button&gt;(R.id.increment_button)
  
          button.setOnClickListener {
              viewModel.incrementLiveData()
          }
  
          var combinedLiveData = lifecycleScope.launch {
              viewModel.combinedLiveData.collectLatest { combined -&gt;
                  Log.d(&quot;&gt;&gt;&quot;, combined)
              }
          }
      }
  }
  
  </code></pre>
  <blockquote>
  <p>ViewModel</p>
  </blockquote>
  <pre><code>class MyViewModel : ViewModel() {
  
      private val liveDataA = MutableLiveData(0)
      private val liveDataB = MutableLiveData(0)
  
      fun incrementLiveData() {
          liveDataA.value = liveDataA.value?.plus(1)
          liveDataB.value = liveDataB.value?.plus(1)
      }
  
      val combinedLiveData = combine(liveDataA.asFlow(), liveDataB.asFlow()) { a, b -&gt;
          &quot;$a $b&quot;
      }
  
  }
  </code></pre>
  <blockquote>
  <p>activity_main</p>
  </blockquote>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      tools:context=&quot;.MainActivity&quot;&gt;
  
      &lt;Button
          android:id=&quot;@+id/increment_button&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:text=&quot;Increment&quot;
          app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
          app:layout_constraintLeft_toLeftOf=&quot;parent&quot;
          app:layout_constraintRight_toRightOf=&quot;parent&quot;
          app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
  
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  
  </code></pre>
  <blockquote>
  <p>Dependencies</p>
  </blockquote>
  <pre><code>
  dependencies {
  
     ...
  
      def lifecycle_version = &quot;2.3.1&quot;
  
      //Coroutines
      implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.1'
      // ViewModel
      implementation &quot;androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version&quot;
      // LiveData
      implementation &quot;androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version&quot;
      // Lifecycles only (without ViewModel or LiveData)
      implementation &quot;androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version&quot;
      // Saved state module for ViewModel
      implementation &quot;androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version&quot;
      // Annotation processor
      kapt &quot;androidx.lifecycle:lifecycle-compiler:$lifecycle_version&quot;
  
  }
  
  
  </code></pre>