Introduction of ReactiveX

Introduction of ReactiveX

5c7e719825efd10bca38b800a8b0d685?s=128

Yuki Funakoshi

February 08, 2016
Tweet

Transcript

  1. Introduction Yuki Funakoshi (@bl_lia) NYLE Inc. Android / iOS Engineer

  2. Appliv use Android Clean Architecture use Dependency Injection use Realm

    use Kotlin … and use RxJava!
  3. Introduction of RxJava

  4. Observable.from(arrayOf( "Introduction", "Appliv App for Android", "Basic of ReactiveX", “ReactiveX

    with Appliv" )).subscribe{ title -> present(title) };
  5. Do you know ReactveX ?

  6. ReactiveX is… Composing asynchronous Event-based programs

  7. Observable Subscriber Subscribe Send Event Async

  8. Observable Subscriber Observable Observable Filter Map Send Event

  9. Observable Observable Subscriber Send Event Send Event

  10. Observable.from(arrayOf(1,2,3,4,5,6,7,8,9,10)) .filter { it % 2 == 0 } .subscribe

    { println(it) } 2 4 6 8 10
  11. val a = Observable.from(arrayOf(1,2,3,4,5)) val b = Observable.from(arrayOf(6,7,8,9,10)) Observable.merge(a, b)

    .filter { it % 2 == 0 } .subscribe { println(it) } 2 4 6 8 10
  12. val a = Observable.from(arrayOf(1,2,3,4,5)) val b = Observable.from(arrayOf(6,7,8,9,10)) Observable.merge(a, b)

    .filter { it % 2 == 0 } .subscribeOn(Schedulers.newThread()) .subscribe { println(it) } 2 4 6 8 10
  13. val a = Observable.create<Int> { subscriber -> subscriber.onNext(2); subscriber.onCompleted(); }

    val b = Observable.from(arrayOf(6,7,8,9,10)) Observable.merge(a, b) .filter { it % 2 == 0 } .subscribe { println(it) } 2 6 8 10
  14. data class Item(val id: String, val title: String) interface ItemService

    { @GET("/api/v2/items") fun items(): Observable<List<Item>> } fun api() { val retrofit = Retrofit.Builder() .baseUrl("http://qiita.com") .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .build(); val service = retrofit.create(ItemService::class.java) service.items().subscribe { it.map { println(it.title) } } } https://gist.github.com/bl-lia/49130ad9d9730c826d02
  15. Cache Control Factory Class cached? Disk Data Store API Data

    Store Disk Data Cache API
  16. interface ItemDataStore { fun items(): Observable<List<Item>> } // Singleton Class

    object ItemCache : ItemDataStore { override fun items(): Observable<List<Item>> { // Return cached items } fun reset(newItemList: List<Item>) { // Replace cached items } fun isCached(): Boolean { // Cache data exists? } } class ItemApi(cache: ItemCache) : ItemDataStore { val cache = cache override fun items(): Observable<List<Item>> { val retrofit = Retrofit.Builder()...build() val service = retrofit.create(ItemService::class.java) return service.items().doOnNext { cache.reset(it) } } } https://gist.github.com/bl-lia/23023d968ab884708f30
  17. class ItemStoreFactory { val cache = ItemCache fun create(): ItemDataStore

    { if (cache.isCached()) { return cache } return ItemApi(cache) } } fun main(args : Array<String>) { ItemStoreFactory().create().items() } https://gist.github.com/bl-lia/23023d968ab884708f30
  18. UI Control with RxJava

  19. None
  20. RxTextView.textChanges(textView) .debounce(1, TimeUnit.SECONDS) .subscribe { search(it) }

  21. https://github.com/bl-lia/RxFb

  22. RxLoginManager.from(loginManager) .logInWithReadPermissions(callbackManager, fragment, permissions) .flatMap { return RxGraphRequest.newMeRequest(accessToken, params) }

    .subscribe { doAnything() }
  23. onCompleted()