Last active
January 16, 2021 21:45
-
-
Save Puneet1796/ac1803b96735c61cbe1a8c86f473681f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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