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 performRequest() { | |
| api.request( | |
| call = service.getUser("jeziellago"), | |
| onSuccess = { user -> // fazer algo com o usuário }, | |
| onFailure = { error -> // exibir o erro } | |
| ) | |
| } |
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
| import kotlinx.coroutines.experimental.CommonPool | |
| import kotlinx.coroutines.experimental.android.UI | |
| import kotlinx.coroutines.experimental.launch | |
| import kotlinx.coroutines.experimental.withContext | |
| import retrofit2.Call | |
| import retrofit2.Retrofit | |
| ... | |
| class Api { | |
| ... |
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 load() = launch(UI) { | |
| try { | |
| val result = withContext(CommonPool) { // work! } | |
| ... | |
| } catch (e: IOException) { // exception que pode gerar | |
| // exception here! | |
| } | |
| } |
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 loadTask() = launch(UI) { // executa na main thread | |
| // executa em uma thread separada | |
| val result1 = withContext(CommonPool) { // work! } | |
| // executa na mesma thread | |
| val result2 = withContext(Unconfined) { // work! } | |
| } |
NewerOlder