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

Diego Almeida de Oliveira

11/28/2019, 9:02 PM
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

Tobi

11/28/2019, 9:57 PM
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

Diego Almeida de Oliveira

11/28/2019, 10:18 PM
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

Tobi

11/28/2019, 10:44 PM
Lol, I should have read it more precisely.
🤣 1
a

Adam Powell

11/28/2019, 10:46 PM
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

Diego Almeida de Oliveira

11/29/2019, 12:24 AM
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

streetsofboston

11/29/2019, 6:25 AM
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

Diego Almeida de Oliveira

11/29/2019, 1:47 PM
Awesome @streetsofboston, I'll take a look later. Thanks
a

ahulyk

02/20/2020, 1:41 PM
Coroutines work quite well in that case. Bth CompositDisposable also works here.
3 Views