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

Intro to reactive programing

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest. →

Intro to reactive programing

Small introduction into rx-java 2.X and it's core element and how to use it

Fernando Franco Gíraldez

December 01, 2021

Transcript

27 slides
  1. Schedule ▸ What is reactive programming ▸ Core elements of

    rx-java ▸ How to test your reactive chains 2
  2. “ The world has move to push; user are waiting

    for us to catch up Lee Campbell - Introduction to Rx
  3. What is reactive programing It’s a library for composing asynchronous

    and event based programs by using observable sequences 6
  4. Core elements \ Observable Observable.create { emitter -> try {

    if (!emitter.isDisposed) { emitter.onNext("rxjava FTW!") emitter.onNext("functional programing FTW!") emitter.onComplete() } } catch (exception: Exception) { emitter.onError(exception) } } 10
  5. Core elements \ Observable Observable.create { emitter -> val receiver

    = object : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) = emitter.onNext(intent) } context.registerReceiver(receiver, IntentFilter()) emitter.setCancellable { context.unregisterReceiver(receiver) } } 11
  6. Core elements \ Operators 17 suggestionTextWatcher.doOnNext { loading.postValue(true) } .switchMapSingle

    { repo.searchSuggestion(it) } .doOnSubscribe { loading.postValue(false) }
  7. Core elements \ Observer val observer = object: Observer<String>{ override

    fun onNext(t: String) {} override fun onError(e: Throwable) {} override fun onComplete() {} override fun onSubscribe(d: Disposable) {} } 18
  8. Core elements \ Observer suggestionTextWatcher .switchMapSingle { repo.searchSuggestion(it) } .subscribe(observer)

    .subscribe { results.postValue(it) } .subscribe({ results -> }, { throwable -> }) .subscribe({ results -> }, { throwable -> }, {}) .subscribeBy( onNext = { results -> }, onError = { throwable -> }, onComplete = {} ) 19
  9. Core elements \ Disposable val disposable = suggestionTextWatcher .subscribe {

    results.postValue(it) } disposable.isDisposed() disposable.dispose() val compositeDisposable = CompositeDisposable() disposable.add(suggestionTextWatcher.subscribe{}) disposable.clear() 20
  10. suggestionTextWatcher.switchMapSingle { repo.searchSuggestion(it) } .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) Core elements \ Threading

    21 To add concurrency into our observable sequence, just indicate to the observable to operate in a particular Scheduler
  11. Testing ▸ How to provide Schedulers? ▸ Make test run

    faster ▸ Use black box testing strategies 24
  12. Testing \ Schedulers val testScheduler = TestScheduler() RxJavaPlugins.setIoSchedulerHandler( scheduler ->

    testScheduler); RxAndroidPlugins.setMainThreadSchedulerHandler( scheduler -> Schedulers.trampoline()); @field:Rule val testSchedulers = TestSchedulerRule.create(); testScheduler.advanceTimeBy(30, TimeUnit.SECONDS); 25

More Decks by Fernando Franco Gíraldez

Other Decks in Programming