https://kotlinlang.org logo
#coroutines
Title
# coroutines
f

Florian

12/04/2020, 1:12 PM
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

Javier

12/04/2020, 1:19 PM
@Manuel Vivo maybe can help you
f

Florian

12/04/2020, 1:22 PM
thank you
m

Manuel Vivo

12/04/2020, 1:23 PM
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

Florian

12/04/2020, 1:25 PM
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

Manuel Vivo

12/04/2020, 1:27 PM
You got it right, only if it references other VM code
f

Florian

12/04/2020, 1:27 PM
ok, thank you 👍
So it seems that switching to a larger scope in the repository also helps avoid memory leaks in the ViewModel?
m

Manuel Vivo

12/04/2020, 1:36 PM
Yeah 💯
🙏 2
4 Views