Should I dispose disposables in a Singleton class ( Android RxJava 2 )
Asked Answered
N

3

15

When I subscribe({}) to an Observable in a Singleton class, do I need to call .dispose() method at some point? and if yes, when and where? because a singleton will remain until the App is running. something like this (Kotlin):

@Singleton
class MySingletonClass @Inject constructor(
    private val api: MyAPIManager
) {

fun fetchData() {

        //subscribed inside the Singleton
        api.service.getSomeDataFromAPI()
        .toRxObservable()
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe({ 
                   //do something internally with response
                   },
                   {
                   //handle error internally
                   })

}

the subscribe() method returns a Disposable.

My main question is: do I need to call dispose() at all? because I think I only can call it when the App is finished or killed, which is not necessary.

Naomanaomi answered 23/7, 2018 at 8:56 Comment(0)
A
5

The (potentially) bigger concern here is that your singleton is doing work outside the lifecycle of an Android component. If your singleton is static or hosted by your Application, then it may be terminated abruptly when your app is in the background. If that's not a problem, then the answer to your question is no, you do not need to dispose your subscription. However, you should still be wary of doing work while your app is running in the background, unless the user expects it. (And if they do, it should probably be in a Service or run on a schedule.) The Application and VM can persist long after the user perceives the app to be "closed", and excessive resource consumption may lead to bad ratings and uninstalls.

Arbor answered 24/7, 2018 at 19:27 Comment(0)
F
0

You can use Uber's autoDispose lib

    myObservable 
.doStuff() 
.as(autoDisposable(this)) // The magic 
.subscribe(s -> ...);
Freidafreight answered 23/7, 2018 at 9:20 Comment(3)
so what is this in the "The Magic" ? The singleton isn't a Provider of a Lifecycle, nor should it be - its a Singleton (in the context of the Component instance). Once a subscription is complete i.e. onComplete or onError it will release any resources anyway. I suggest before answering with a link to a library you understand what you're copy/pasting as an answer and give a complete solution, rather than a incorrect one.Ertha
You can give lifecycle owner as a parameter do you know that Mark:)Freidafreight
You know sarcasm is the lowest form of wit. I take it from this you can't actually defend your answer then.Ertha
I
-2

Yes, usually I dispose it in stop function of presenter, which is called in onStop() method of activity

Icosahedron answered 23/7, 2018 at 9:3 Comment(3)
But here I didn't subscribe in an Activity, it's only subscribed in Singleton itselfNaomanaomi
Answer to your main question is why not))? Because you will have memory leak, if you do not unsubscribe your singleton class. Am I right?))Icosahedron
I'm not sure but I think memory leak can only happen for activities and fragments because actually, it happens when you subscribe to an observer, then before getting response from observer, finish the activity or the fragment but because you subscribed to an observer, it keeps a reference of activity or fragment, then memory leak happens! (if user open that activity again, android create a new instance for that activity, but Java Garbage Collector don't collect the old activity, because there is still a reference for old activity in the observer), but this story can't happen for singletons.Naomanaomi

© 2022 - 2024 — McMap. All rights reserved.