Skip to content

Instantly share code, notes, and snippets.

@truedem
Created October 24, 2018 00:59
Show Gist options
  • Save truedem/37f9997eca977cd5cbf977c63c0818f5 to your computer and use it in GitHub Desktop.
Save truedem/37f9997eca977cd5cbf977c63c0818f5 to your computer and use it in GitHub Desktop.
Safely set value for MutableLiveData
// 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