Hello ! I'm using retrofit to get some data from a...
# android
c
Hello ! I'm using retrofit to get some data from a website. What is the best practice to return data from a call
onResponse
? Should I: * pass a callback and send the data back through it? * use the https://github.com/gildor/kotlin-coroutines-retrofit library? * pass a LiveData as parameter to the
call
function and post the value to it?
g
It depends on your use case
you can use my library if you plan to use coroutines and you already have some services with
Call<T>
type Soon Retrofit will support
suspend
functions out of the box
c
I do use them already. I might go for your library then, as I like to have a method with a clear return type.
g
clear return type will be with support of suspend functions
for now you can use
Call<T>
and my library Or you can use
Deffered<T>
and this library https://github.com/JakeWharton/retrofit2-kotlin-coroutines-adapter
c
Oh cool thanks. I think I already used that one once as well. Thanks for all those infos.
So there's no reason against using a LiveData for now?
I will postpone the refactor for a bit if that's the case.
g
I don’t see why do you need LiveData with coroutines for network requests
because coroutines already enforce you to explicitly specify lifecycle
c
I'm using coroutines for the project, but not for networking right now. Sorry I should have been clearer.
g
I believe coroutines is the best option for networking
very clear code, no callbacks, lifecycle safe out of the bbox
c
Oh, if that is the case, I might as well get to it now. Do you have any particular reason for saying so?
Thanks.
g
Also approach with LiveData is pretty different, because LiveData is not a promise like abstraction. Also LiveData as observable can be used together with coroutines