Overview
為什麼要使用
為什麼要切換Thread?
Callback vs Coroutine
Callback
class LoginViewModel(){
fun login(){
//don't do this in prodcut code
task1 = thread {
respority.login(account, password, object: Callback<User> {
fun onSuccess(user: User) {
}
fun onFailure(exceptione: Exception) {
}
})
}
}
onCleared(){
task1.cancel()
}
}
class Respority {
fun login(account: String, password: String, result: Callback<User>) {
//do some connection
if(task is successful) {
result.onSuccess(user)
} else {
result.onFaliure(exception)
}
}
}Coroutine
Callback
Coroutine
參考
Last updated