Skip to content

Instantly share code, notes, and snippets.

@jeziellago
Created August 13, 2018 00:43
Show Gist options
  • Save jeziellago/3a439932890d79df06448d84a26625ec to your computer and use it in GitHub Desktop.
Save jeziellago/3a439932890d79df06448d84a26625ec to your computer and use it in GitHub Desktop.
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 <T> request(call: Call<T>,
onSuccess:(T?)-> Unit,
onFailure:(IOException)-> Unit) = launch(UI) {
try {
val result = withContext(CommonPool) { call.execute() }
onSuccess(result.body())
} catch (exception: IOException) {
onFailure(exception)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment