Если вы используете Kotlin, вы можете сделать
val compositeDisposable = CompositeDisposable()
var result: MYEntry? = null
val disposable = myvar.observe(MyEntry::class.java)
.one(entryId)
.applySchedulers()
.applyOpBeforeAfter(showProgress, hideProgress)
.subscribeBy(
onNext = {
result = it
},
onError = {
Timber.e(e)
},
onComplete = {
result?.let {
val something = Something(title = it.getField<String>(MyConstants.TITLE_KEY),
somethingImageAsset = it.getField<MyAsset>(MyConstants.IMAGE_KEY),
bodyText = it.getField<String>(MyConstants.BODY_TEXT_KEY))
view?.updateMySpace(something)
}
}
)
compositeDisposable.add(disposable)
compositeDisposable.dispose() // Dispose it in onStop() or onDestroy()
ИЛИ
val compositeDisposable = CompositeDisposable();
var result: MYEntry? = null
val disposable = myvar.observe(MyEntry::class.java)
.one(entryId)
.applySchedulers()
.applyOpBeforeAfter(showProgress, hideProgress)
.subscribe(
{
result = it
},
{
Timber.e(e)
},
{
result?.let {
val something = Something(title = it.getField<String>(MyConstants.TITLE_KEY),
somethingImageAsset = it.getField<MyAsset>(MyConstants.IMAGE_KEY),
bodyText = it.getField<String>(MyConstants.BODY_TEXT_KEY))
view?.updateMySpace(something)
}
}
)
compositeDisposable.add(disposable)
compositeDisposable.dispose() // Dispose it in onStop() or onDestroy()
Для CompositeDisposable:
private var compositeDisposable = CompositeDisposable()
fun getCompositeDisposable(): CompositeDisposable {
if (compositeDisposable.isDisposed)
compositeDisposable = CompositeDisposable()
return compositeDisposable
}
fun addDisposable(disposable: Disposable) {
getCompositeDisposable().add(disposable)
}
fun dispose() {
getCompositeDisposable().dispose()
}