Skip to content

Instantly share code, notes, and snippets.

@iammert
Last active Jan 10, 2021
Embed
What would you like to do?
croutine_intro
api.fetchUser(object: Callback{
fun onResponse(userInfo: UserInfo){
userTextView.text = userInfo.name
}
})
api.fetchUser(object: Callback{
fun onResponse(userInfo: UserInfo){
api.fetchLastOnlineTime(userInfo.userId, object: Callback{
fun onResponse(userActivity: UserActivity){
userTextView.text = userInfo.name
userLastOnlineTime.text = userActivity.time
}
})
}
})
suspend fun fetchUser(): UserInfo {
apiService.fetchUser()
}
suspend fun fetchLastOnlineTime(userId: String) : UserActivity {
apiService.fetchLastOnlineTime(userId)
}
suspend fun fetchData() = withContext(Dispatcher.IO){
val userInfo = apiService.fetchUser()
val userActivity = apiService.fetchLastOnlineTime(userId)
val mappedData = map(userInfo, userActivity)
mappedData
}
GlobalScope.launch(Dispatchers.Main) {
val userInfoAndActivity = fetchData()
statusTextView.text = userInfoAndActivity.lastOnlineTime
}
api.fetchUser()
.doOnNext(userInfo -> userNameText.text = userInfo.name)
.flatMap(userInfo -> api.fetchLastOnlineTime(userInfo.userId))
.doOnNext(userActivity -> userActivity.text = userActivity.name)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe()
val userInfo = api.fetchUser()
val userActivity = api.fetchLastOnlineTime(userInfo.userId)
userTextView.text = userInfo.name
userLastOnlineTime.text = userActivity.time
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment