Created
October 24, 2018 00:59
-
-
Save truedem/37f9997eca977cd5cbf977c63c0818f5 to your computer and use it in GitHub Desktop.
Safely set value for MutableLiveData
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
// to avoid this situation: https://kinnrot.github.io/live-data-pitfall-you-should-be-aware-of/ | |
fun <T> MutableLiveData<T>.safeSetValue(newValue: T) { if(Thread.currentThread() == Looper.getMainLooper().thread) value = newValue else postValue(newValue) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment