-
-
Save Boken/55814f98cbe71895687f13583880baef to your computer and use it in GitHub Desktop.
Concatenate LiveData
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
fun <T, R> LiveData<T>.then(callback: (T) -> LiveData<R>): LiveData<R> { | |
return Transformations.switchMap(this, { | |
result(it) | |
}) | |
} |
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
/** | |
* @param owner is needed to observe the final response and call the view | |
*/ | |
fun promise(owner: LifecycleOwner) { | |
func1().then { | |
Log.d("PROMISE", "first $it") | |
func2() | |
}.then { | |
Log.d("PROMISE", "second $it") | |
func1() | |
}.observe(owner, Observer { | |
Log.d("PROMISE", "observe $it") | |
}) | |
} | |
/** | |
* Example functions representing networking requests or disk read threads | |
*/ | |
fun func1(): LiveData<String> { | |
val result = MutableLiveData<String>() | |
Handler().postDelayed({ | |
result.postValue("foo") | |
}, 2000) | |
return result | |
} | |
fun func2(): LiveData<String> { | |
val result = MutableLiveData<String>() | |
Handler().postDelayed({ | |
result.postValue("bar") | |
}, 2000) | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment