Last active
February 1, 2019 08:56
-
-
Save ch4vi/0f7893bd830f195a99881a30b3cb7640 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 | |
} |
What's result(it)
here? I know what it
is, but I can't figure where you get result
from?
Looks like result
was renamed to callback
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looks promising