Last active
April 17, 2019 08:20
-
-
Save krzdabrowski/1966d6b8f687a2e0abe807057373b4f3 to your computer and use it in GitHub Desktop.
This file contains 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
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