Skip to content

Instantly share code, notes, and snippets.

View jeziellago's full-sized avatar
🇧🇷

Jeziel Lago jeziellago

🇧🇷
View GitHub Profile
fun performRequest() {
api.request(
call = service.getUser("jeziellago"),
onSuccess = { user -> // fazer algo com o usuário },
onFailure = { error -> // exibir o erro }
)
}
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 {
...
fun load() = launch(UI) {
try {
val result = withContext(CommonPool) { // work! }
...
} catch (e: IOException) { // exception que pode gerar
// exception here!
}
}
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! }
}