Skip to content

Instantly share code, notes, and snippets.

Working from home

Puneet Verma Puneet1796

Working from home
View GitHub Profile
// This is the Java equivalent function for GetLiveDataValue.kt gist.
// Refer to link below
public static <T> T getBlockingValue(@NotNull final LiveData<T> liveData) throws InterruptedException {
final Object[] value = new Object[1];
final CountDownLatch latch = new CountDownLatch(1);
Observer<T> innerObserver = new Observer<T>() {
View GetLiveDataValue.kt
// This extension function will get the value from LiveData.
// It should be used in testing.
// For Queries returning values wrapped around LiveData.
fun <T> LiveData<T>.getValueBlocking(): T? {
var value: T? = null
val latch = CountDownLatch(1)
val innerObserver = Observer<T> {
value = it