Created
February 2, 2020 23:54
-
-
Save felipefpx/fd53640a829f44a0dd3f07fbce12486e 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
import androidx.lifecycle.LiveData | |
import androidx.lifecycle.Observer | |
import java.util.concurrent.CountDownLatch | |
import java.util.concurrent.TimeUnit | |
fun <T> LiveData<T>.blockingObserve(): T? { | |
val countDownLatch = CountDownLatch(1) | |
var result: T? = null | |
val observer = Observer<T> { | |
result = it | |
countDownLatch.countDown() | |
} | |
observeForever(observer) | |
countDownLatch.await(2, TimeUnit.SECONDS) | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment