1.0.6
馃帀 Released a new version 1.0.6! 馃帀
What's New?
- Added a
Disposableinterface anddisposable()extension for canceling tasks when we want. - Added
DisposableCompositeis that a disposable container that can hold onto multiple other disposables. - Added
joinDisposableextensions toDataSourceandCallfor creating a disposable and add easily.
Disposable in Call
We can cancel the executing works using a disposable() extension.
val disposable = call.request { response ->
// skip handling a response //
}.disposable()
// dispose the executing works
disposable.dispose()And we can use CompositeDisposable for canceling multiple resources at once.
class MainViewModel constructor(disneyService: DisneyService) : ViewModel() {
private val disposables = CompositeDisposable()
init {
disneyService.fetchDisneyPosterList()
.joinDisposable(disposables) // joins onto [CompositeDisposable] as a disposable.
.request {response ->
// skip handling a response //
}
}
override fun onCleared() {
super.onCleared()
if (!disposables.disposed) {
disposables.clear()
}
}
}Disposable in DataSource
We can make it joins onto CompositeDisposable as a disposable using the joinDisposable function. It must be called before request() method. The below example is using in ViewModel. We can clear the CompositeDisposable in the onCleared() override method.
private val disposable = CompositeDisposable()
init {
disneyService.fetchDisneyPosterList().toResponseDataSource()
// retry fetching data 3 times with 5000L interval when the request gets failure.
.retry(3, 5000L)
// joins onto CompositeDisposable as a disposable and dispose onCleared().
.joinDisposable(disposable)
.request {
// ... //
}
}
override fun onCleared() {
super.onCleared()
if (!disposable.disposed) {
disposable.clear()
}
}