https://kotlinlang.org logo
#android
Title
# android
c

CamilleBC

12/05/2018, 10:33 AM
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

gildor

12/05/2018, 10:34 AM
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

CamilleBC

12/05/2018, 10:36 AM
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

gildor

12/05/2018, 10:37 AM
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

CamilleBC

12/05/2018, 10:39 AM
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

gildor

12/05/2018, 10:40 AM
I don’t see why do you need LiveData with coroutines for network requests
because coroutines already enforce you to explicitly specify lifecycle
c

CamilleBC

12/05/2018, 10:41 AM
I'm using coroutines for the project, but not for networking right now. Sorry I should have been clearer.
g

gildor

12/05/2018, 10:41 AM
I believe coroutines is the best option for networking
very clear code, no callbacks, lifecycle safe out of the bbox
c

CamilleBC

12/05/2018, 10:42 AM
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

gildor

12/05/2018, 10:44 AM
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
4 Views