Skip to content

Instantly share code, notes, and snippets.

@franciscojunior
Forked from arekolek/LiveData.kt
Created July 15, 2018 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save franciscojunior/ec78a3202a0d1955c68fb7efc7c82459 to your computer and use it in GitHub Desktop.
Save franciscojunior/ec78a3202a0d1955c68fb7efc7c82459 to your computer and use it in GitHub Desktop.
LiveData extension property for use in unit tests
val <T> LiveData<T>.blockingValue: T?
get() {
var value: T? = null
val latch = CountDownLatch(1)
observeForever {
value = it
latch.countDown()
}
if (latch.await(2, TimeUnit.SECONDS)) return value
else throw Exception("LiveData value was not set within 2 seconds")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment