Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Puneet Verma Puneet1796

🏠
Working from home
View GitHub Profile
View GetLiveDataValue.java
// This is the Java equivalent function for GetLiveDataValue.kt gist.
// Refer to link below
// https://gist.github.com/Puneet1796/ac1803b96735c61cbe1a8c86f473681f
@SuppressWarnings("unchecked")
@Nullable
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>() {
@Override
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.
@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()