android, how to dispose the observer in case there is no context, or lifeCycle object

Viewed 234

In android app and in the place there is no access to context and lifeCycle object, and this code is in independent class not live in fragment/activity/ViewMode, so no help from onDestroy()

what is the right way to dispose the observer?

it could add .autoDispose(lifecycleOwner.scope()) if there is access to the lifecycleOwner but not able to in this case.

interface IUpdate {
        fun onUpdateComplete(isSuccess: Boolean, exception: Throwable?)
    }

fun updateTopic(data: Data, cb: IUpdate? = null) {
        var disposable: Disposable? = null
        disposable = dataRepository.updateDatabase(data) // this one returns a Completable, and actually go fetch from remote and update database, may take a bit time
            .compose(Transformers.completableIOToMain())
            //.autoDispose(lifecycleOwner.scope())//<=== no access to lifecycleOwner
            .subscribe({
                disposable?.dispose(). // <=== is it right to do here
                cb?.onUpdateComplete(true, null)
            }, {
                disposable?.dispose() // <=== is it right to do here
                cb?.onUpdateComplete(false, it)
            })
    }
0 Answers
Related