Hello everyone. I published this article that talk...
# android
d
Hello everyone. I published this article that talks the guide to app architecture provided by Google, and how using it without extra care might lead to ViewModels being leaked. I'd appreciate if you could take some minutes to read it and give me some feedback. Thanks :) https://cheesecakelabs.com/blog/android-architecture-components-viewmodel-leak/?utm_source=linkedin&utm_medium=post&utm_campaign=blogpost-viewmodel
t
Sth. you could consider as well for your post is the scope of the repository. If you have a repository that has the same scope as the view model, then there is less likely a chance you leak it. In your example it looks like the repository has a larger scope, which I'd consider as bad practice in most of the cases 🤷 .
d
Hey Tobias, thx for your suggestion. I actually briefly mention what you said as you can see in the screenshot. I decided to use Repository as a Singleton only because in the guide to app architecture it uses Dagger's
@Singleton
annotation, which can make it have same scope as a Singleton using
object
.
đź‘Ť 1
Ops, I just realized the last sentence is incomplete, thanks to you :)
t
Lol, I should have read it more precisely.
🤣 1
a
Coroutines handle the case you describe here nicely through job cancellation if the repository calls are suspend functions
The ViewModel CoroutineScope as provided will cancel the jobs launched into it on clear
d
That's really good to know. I haven't use Coroutines yet except in a small test, but I'll soon give it a try and maybe update my article with it. Thanks @Adam Powell
đź‘Ť 1
No worries @Tobi
s
For lifecycle handling using Coroutines, you may find my blog post interesting. “How can we use CoroutineScopes in Kotlin?” by Anton Spaans https://link.medium.com/IRyDquHh01
d
Awesome @streetsofboston, I'll take a look later. Thanks
a
Coroutines work quite well in that case. Bth CompositDisposable also works here.