Skip to content

Instantly share code, notes, and snippets.

@ahinchman1
Created October 28, 2022 06:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahinchman1/3dbf10ef5b588c5de81e72d20c84bac3 to your computer and use it in GitHub Desktop.
Save ahinchman1/3dbf10ef5b588c5de81e72d20c84bac3 to your computer and use it in GitHub Desktop.
Both leaks fixed
class ActivityPresenter @Inject constructor(
private val repository: SomeRepository
) : BasePresenter<ActivityView>() {
/* removed for brevity */
private val viewDisposable = CompositeDisposable()
override fun attachView(view: ActivityView?) {
super.attachView(view)
val disposable = repository.doSomeHeavyWorkAndReturnState()
.subscribeOn(Schedulers.io()) // send work to background thread
.observeOn(AndroidSchedulers.mainThread()) // return results to main thread
.subscribe { viewState.render(it) }
viewDisposable.add(disposable)
}
override fun detachView(retainInstance: Boolean) {
super.detachView(retainInstance)
compositeDisposable.clear() // calls to dispose of all disposables end of view life
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment