Skip to content

Instantly share code, notes, and snippets.

@felipefpx
Created February 2, 2020 23:54
Show Gist options
  • Save felipefpx/fd53640a829f44a0dd3f07fbce12486e to your computer and use it in GitHub Desktop.
Save felipefpx/fd53640a829f44a0dd3f07fbce12486e to your computer and use it in GitHub Desktop.
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
fun <T> LiveData<T>.blockingObserve(): T? {
val countDownLatch = CountDownLatch(1)
var result: T? = null
val observer = Observer<T> {
result = it
countDownLatch.countDown()
}
observeForever(observer)
countDownLatch.await(2, TimeUnit.SECONDS)
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment