Skip to content

Instantly share code, notes, and snippets.

@kichiemon
Last active January 31, 2020 04:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kichiemon/6238e6df9ba061a52cb3074cf443be58 to your computer and use it in GitHub Desktop.
Save kichiemon/6238e6df9ba061a52cb3074cf443be58 to your computer and use it in GitHub Desktop.
fun <A, B> LiveData<A>.combineLatestWith(other: LiveData<B>): LiveData<Pair<A, B>> {
val mediator = MediatorLiveData<Pair<A, B>>()
mediator.addSource(this) {
it?.let { thisValue ->
other.value?.let { otherValue ->
mediator.value = Pair(thisValue, otherValue)
}
}
}
mediator.addSource(other) {
it?.let { otherValue ->
this.value?.let { thisValue ->
mediator.value = Pair(thisValue, otherValue)
}
}
}
return mediator
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment