DisposeOnDestroy
RxJava2 helper to dispose Disposables before leaking stuff
Usage
Observable::disposeOnDestroy(Activity)
Single::disposeOnDestroy(Activity)
Maybe::disposeOnDestroy(Activity)
Completable::disposeOnDestroy(Activity)
class MyActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.my_activity)
makeApiCall()
}
private fun makeApiCall() {
myRetrofitApi.call()
.subscribeOn(io())
.observeOn(mainThread())
.disposeOnDestroy(this)
.subscribe(this::displayResults, this::displayError)
}
// ...
}
Also works with Fragment
s from support-v4.
Observable::disposeOnDestroyView(Fragment)
Single::disposeOnDestroyView(Fragment)
Maybe::disposeOnDestroyView(Fragment)
Completable::disposeOnDestroyView(Fragment)
class MyFragment : Fragment() {
// ...
private fun onSearchClick(query: String) {
searchApi.call(query)
.subscribeOn(io())
.observeOn(mainThread())
.disposeOnDestroyView(this)
.subscribe(this::displayResults, this::displayError)
}
// ...
}
See demo module for working examples or some random commit to see how to apply this little lib.
TODO: add examples with logic extracted into plain classes.
Gradle
dependencies {
compile 'pl.mg6.rxjava2.disposeondestroy:core:0.1.0'
compile 'pl.mg6.rxjava2.disposeondestroy:support:0.1.0'
}