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.
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
oronError
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