I have a question regarding this article: <https:/...
# coroutines
f
I have a question regarding this article: https://medium.com/androiddevelopers/coroutines-patterns-for-work-that-shouldnt-be-cancelled-e26c40f142ad My question: If the repository method would not contain
doSomeOtherWork
, we could also switch to the `applicationScope`/`externalScope` in the ViewModel directly, right?
j
@Manuel Vivo maybe can help you
f
thank you
m
Yeah, you could. Although you could also argue that it’s the repo responsibility to know if that operation should outlive the scope where it’s been called or not
Usually repos (and the data layer in general) contains the business logic of the app. Centralising those decisions in the repo layer makes it easier to reason about
In that way, callers don’t need to worry about how long each operation should live, they do a best effort by calling it in the scope they live (in this case, viewModelScope, which is what the ViewModel cares about)
f
Makes sense, thank you
👍 1
Does using the larger-than-ViewModel-scope inside the ViewModel introduce memory leaks if I reference one of the ViewModel's properties?
m
You got it right, only if it references other VM code
f
ok, thank you 👍
So it seems that switching to a larger scope in the repository also helps avoid memory leaks in the ViewModel?
m
Yeah 💯
🙏 2