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.

Amir Khorsandi
  • 3,542
  • 1
  • 34
  • 38

3 Answers3

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.

StackOverthrow
  • 1,158
  • 11
  • 23
0

You can use Uber's autoDispose lib

    myObservable 
.doStuff() 
.as(autoDisposable(this)) // The magic 
.subscribe(s -> ...);
Ahmet Zorer
  • 122
  • 1
  • 8
  • 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. – Mark Jul 25 '18 at 00:37
  • You can give lifecycle owner as a parameter do you know that Mark:) – Ahmet Zorer Jul 25 '18 at 04:30
  • You know sarcasm is the lowest form of wit. I take it from this you can't actually defend your answer then. – Mark Jul 27 '18 at 00:08
-2

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

Asset Bekbossynov
  • 1,633
  • 3
  • 13
  • 25
  • But here I didn't subscribe in an Activity, it's only subscribed in Singleton itself – Amir Khorsandi Jul 23 '18 at 09:25
  • 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?)) – Asset Bekbossynov Jul 24 '18 at 04:47
  • 4
    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. – Amir Khorsandi Jul 24 '18 at 09:54