Last active
July 15, 2018 14:08
-
-
Save arekolek/e9e0d050cdd6ed16cd7dd9183eee62c0 to your computer and use it in GitHub Desktop.
LiveData extension property for use in unit tests
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
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