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

bhatnagarm

02/27/2020, 11:34 AM
What is the general opinion of Using coroutines inside ViewModel. As blogged by @Sean McQuillan [G] in his blog https://medium.com/androiddevelopers/coroutines-on-android-part-ii-getting-started-3bff117176dd ViewModelScope automatically cancels any coroutine started inside ViewModel after onCleared is called
How far my understanding is same would be the case even without using coroutines. So in case where ViewModel observes LiveData returned from repository class. Now if user closes the app and by that time the no data is returned by repository. Wouldn't it also endure that the same call would be suspended.
I'm bit confused here and trying to understand the proper usecase where I can take advantage from coroutines.
z

Zach Klippenstein (he/him) [MOD]

02/27/2020, 2:52 PM
I would recommend not thinking in terms of “how can i use coroutines” but starting with an actual problem you need to solve, and determining if and how coroutines might be a useful tool to help solve it.
💯 3
👍 1
b

bhatnagarm

02/28/2020, 11:15 AM
@Zach Klippenstein (he/him) [MOD] thanks for suggestion, Well I was actually trying to look from the same perspective
but unfortunately couldn't corelate the pros and cons of having it or not
z

Zach Klippenstein (he/him) [MOD]

02/28/2020, 10:33 PM
Can you describe your use case? Then we can discuss pros/cons of using coroutines for that case.
b

bhatnagarm

03/07/2020, 9:27 AM
You can consider a simple MVVM flow as showcased in Jetpack View -> ViewModel -> Repository -> either Network or Local
Say the app is fetching data from network so at each level the data is wrapped into LiveData and exposed downwards teh hierarchy
At ViewModel level we have Transformations observing the LiveData exposed by Repository. So in a case like why would one need to have coroutines is what I was looking at to undestand
@Zach Klippenstein (he/him) [MOD]?
z

Zach Klippenstein (he/him) [MOD]

03/09/2020, 4:27 AM
It sounds like you'd probably benefit from something other than livedata, which was never really intended to be used as a generic async primitive outside of the UI. Are you actually working with streams of data, or just individual calls? If the former, you could use RxJava (Flow is too immature still unless you need multiplatform support). If the latter, you can replace your livedatas with suspend functions which have a much simpler API and compose nicer.
You can still use livedata on the last hop, to expose your streams or suspend functions to your UI layer
b

bhatnagarm

03/09/2020, 4:30 AM
Well my case is one shot request to get data from server and have it displayed in UI
Also if I use suspend func so the hierarchy should look something like this ApiInterface (suspend func) -> Repository (suspend func) -> finally ViewModel with a LiveData scope calling repository func
👍 1
Please correct me in got it wrong
z

Zach Klippenstein (he/him) [MOD]

03/09/2020, 5:12 AM
That looks right to me
b

bhatnagarm

03/09/2020, 5:31 AM
Thanks man, well one more thing I would like to address here is wrapping the data from Repository into NetworkBoundResource obj,
Wanted to check in case you implemented something like this
z

Zach Klippenstein (he/him) [MOD]

03/09/2020, 3:27 PM
I've never heard of
NetworkBoundResource
- is that type from a library?
Have a look
z

Zach Klippenstein (he/him) [MOD]

03/09/2020, 4:41 PM
Ah, it's a simple implementation of the repository pattern. You could implement this using the
liveData
builder to create LiveDatas from your suspend functions, but I would recommend using something like github.com/dropbox/Store instead - it is an actual repository library and has first-class coroutine support
b

bhatnagarm

03/09/2020, 5:54 PM
But isn't it contradictory having LiveData exposed from the suspend func as it would break the core concept. As per my understanding from a ViewModel we can start a coroutine with a LiveData scope and call to repo to get data
By this way there won't we any need of LiveData shopping there chain starting from ViewModel except LiveData scoped Coroutine upto ApiInterface
z

Zach Klippenstein (he/him) [MOD]

03/09/2020, 6:01 PM
But isn’t it contradictory having LiveData exposed from the suspend func as it would break the core concept.
You’re right, but it looks like
NetworkBoundResource
is using the `LiveData`s from its abstract methods as single-emission futures, not as streams. This is very much not what
LiveData
was designed for, I would consider it a pretty big smell. Again, I’d much rather use a real repository library like Store than copy/pasting (likely obsolete) sample code.
b

bhatnagarm

03/16/2020, 7:26 PM
I echo with you on that. Thanks mate
So in general what are the sources you look for in order to have best or atleast the one in trend- architectural patterns. I've been usually looking into google samples and trying to follow the same in my projects but the scenarios like this puts me in doubt did I made the right decision or may be I should start exploring other options as well