Skip to content

Instantly share code, notes, and snippets.

@Puneet1796
Last active January 16, 2021 21:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Puneet1796/ac1803b96735c61cbe1a8c86f473681f to your computer and use it in GitHub Desktop.
Save Puneet1796/ac1803b96735c61cbe1a8c86f473681f to your computer and use it in GitHub Desktop.
// This extension function will get the value from LiveData.
// It should be used in testing.
// For Queries returning values wrapped around LiveData.
@Throws(InterruptedException::class)
fun <T> LiveData<T>.getValueBlocking(): T? {
var value: T? = null
val latch = CountDownLatch(1)
val innerObserver = Observer<T> {
value = it
latch.countDown()
}
observeForever(innerObserver)
latch.await(2, TimeUnit.SECONDS)
return value
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment