AutoDispose

 AutoDispose

Rx Ja Night #2
TwitterID変えました。@lvla0805 -> @MoyuruAizawa

5f533179da1c82722252cbcb93e7356f?s=128

Moyuru Aizawa

June 12, 2017
Tweet

Transcript

  1. 4.

    var disposable: Disposable? = null 
 override fun onCreate(…) {

    …
 disposable = Flowable.interval(1, TimeUnit.SECONDS)
 .subscribe { sec -> textView.text = "${sec}s" }
 } 
 override fun onDestroy() {
 disposable?.dispose()
 } Disposable
  2. 5.

    val disposables = CompositeDisposable() 
 override fun onCreate(…) { …


    Flowable.interval(1, TimeUnit.SECONDS)
 .subscribe { sec -> textView.text = "${sec}s" }
 .addTo(disposables)
 } 
 override fun onDestroy() {
 disposables.dispose()
 super.onDestroy()
 } CompositeDisposable
  3. 11.

    ‣ lifecycle() ‣ returns an Observable of lifecycle events. ‣

    This should be backed by a BehaviorSubject or something similar ‣ correspondingEvents() ‣ a mapping of events to corresponding ones. ‣ i.e. CREATE -> DESTROY ‣ peekLifecycle() ‣ returns the current lifecycle state of the object. AutoDispose
  4. 12.

    abstract class RxActivity : AppCompatActivity(),
 LifecycleScopeProvider<ActivityLifeCycle> {
 
 private val

    lifecycle = BehaviorSubject.create<ActivityLifeCycle>()
 private lateinit var currentEvent: ActivityLifeCycle
 
 override fun lifecycle(): Observable<ActivityLifeCycle> = lifecycle
 
 override fun correspondingEvents(): Function<ActivityLifeCycle, ActivityLifeCycle> {
 return Function { lastEvent: ActivityLifeCycle ->
 when (lastEvent) {
 CREATE -> DESTROY
 else -> throw OutsideLifecycleException("Activity was destroyed")
 }
 }
 }
 
 override fun peekLifecycle() = currentEvent
 } AutoDispose
  5. 13.

    class MainActivity : RxActivity() {
 …
 override fun onCreate(…) {


    Flowable.interval(1, TimeUnit.SECONDS)
 .to(FlowableScoper<Long>(this))
 .subscribe { sec -> textView.text = "${sec}s" }
 }
 } AutoDispose
  6. 14.