Upgrade to Pro — share decks privately, control downloads, hide ads and more …

AutoDispose

 AutoDispose

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

Moyuru Aizawa

June 12, 2017
Tweet

More Decks by Moyuru Aizawa

Other Decks in Technology

Transcript

  1. 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. 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. ‣ 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. 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. class MainActivity : RxActivity() {
 …
 override fun onCreate(…) {


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