Skip to content

Instantly share code, notes, and snippets.

@krzdabrowski
Last active April 17, 2019 08:20
Show Gist options
  • Save krzdabrowski/1966d6b8f687a2e0abe807057373b4f3 to your computer and use it in GitHub Desktop.
Save krzdabrowski/1966d6b8f687a2e0abe807057373b4f3 to your computer and use it in GitHub Desktop.
inline fun <reified T: Any> fetchData(crossinline call: (SpaceXService) -> Deferred<Response<List<T>>>): LiveData<List<T>> {
val result = MutableLiveData<List<T>>()
CoroutineScope(Dispatchers.IO).launch {
val request = call(service)
withContext(Dispatchers.Main) {
try {
val response = request.await()
if (response.isSuccessful) {
result.value = response.body()
} else {
Timber.d("Error occurred with code ${response.code()}")
}
} catch (e: HttpException) {
Timber.d("Error: ${e.message()}")
} catch (e: Throwable) {
Timber.d("Error: ${e.message}")
}
}
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment