$30 off During Our Annual Pro Sale. View Details »

Vietnam MobileDay GoLive 2020

Vietnam MobileDay GoLive 2020

## Reactive Programming in Kotlin/Android

- What is reactive programming?
- How do we realize reactive programming concepts with Kotlin/Android
- Some common patterns we use in Android development with reactive programming

Toan Tran

August 17, 2020
Tweet

More Decks by Toan Tran

Other Decks in Technology

Transcript

  1. MobileDay GoLive 2020

    View Slide

  2. About speaker
    Hello! I’m Toan
    VP, Mobile @Lazada (check out our opening positions!)
    https://toan.mobi
    toantran-ea
    toan_mobi

    View Slide

  3. Reactive Programming in Kotlin|Android
    1. The ultimate goal of app development.
    2. Introducing Reactive Programming.
    3. Realize RP with Kotlin in Android.
    4. Some common patterns we may use daily.
    5. Key-takeaways.

    View Slide

  4. 1. Let’s talk about application development
    Information
    Data
    User interface
    Application

    View Slide

  5. 1. Let’s talk about application development
    Information
    Data
    User interface
    Application
    Users

    View Slide

  6. 1. Let’s talk about application development
    Application
    Speed
    Effective
    Efficient
    Scalable

    View Slide

  7. 1. Introducing Reactive Programming
    1. A programming paradigm.
    2. Based on 3 pillars:
    ○ Data streams.
    ○ Asynchronous processing.
    ○ Functional programming.

    View Slide

  8. 1. Introducing Reactive Programming
    Data streams: sequence of events. It could be anything!

    View Slide

  9. 1. Introducing Reactive Programming
    Observer Observable
    Events
    Subscription

    View Slide

  10. 1. Introducing Reactive Programming
    Reactive Programming vs …
    Val data = getDataFromStream()
    If (data is matched condition) {
    ui.display()
    }
    getDataFromStream().
    .filter( condition )
    .subscribe {
    ui.display()
    }

    View Slide

  11. 1. Introducing Reactive Programming
    Reactive Programming vs …
    Val remote = getDataFromStream()
    // blocking call
    Val local = readFromLocalDB()
    // blocking call
    If (remote is matched condition 1
    AND local matched condition 2 ) {
    ui.display()
    }
    getDataFromStream().
    concat(readFromLocalDB)
    .filter( condition1 AND condition 2 )
    .subscribe {
    ui.display()
    }

    View Slide

  12. 1. Introducing Reactive Programming
    Reactive Programming vs …
    Callback remote = getDataFromStream()
    Callback local = readFromLocalDB()
    remote.execute(THREAD_IO).onResult { res1 ->
    local.excecute(THREAD_IO).onResult {res2 ->
    if (condition 1 and condition 2) {
    runOnnUIThread {
    ui.display()
    }
    }
    }
    }
    getDataFromStream().
    concat(readFromLocalDB)
    .subcribeOn(THREAD_IO)
    .filter( condition1 AND condition 2 )
    .observerOn(UI_THREAD)
    .subscribe {
    ui.display()
    }

    View Slide

  13. 3. Realize RP in Kotlin/Android
    • http://reactivex.io/
    • RxJava, RxJS, RxSwift, etc
    • Early adopted by big names: Netflix,
    Microsoft, GitHub, SoundCloud, etc

    View Slide

  14. 3. Realize RP in Kotlin/Android
    Main functionalities of Rx family

    View Slide

  15. 3. Realize RP in Kotlin/Android

    View Slide

  16. 3. Realize RP in Kotlin/Android
    RxJava + Android

    View Slide

  17. 3. Realize RP in Kotlin/Android
    RxJava:
    - Create
    - Combine
    - Subscribe
    RxAndroid:
    - Android
    threading

    View Slide

  18. 3. Realize RP in Kotlin/Android

    View Slide

  19. 3. Realize RP in Kotlin/Android
    Coroutine
    Flow

    View Slide

  20. 4. Common patterns
    Creating patterns: To create observables
    - Create
    - Defer
    - From
    - Just

    View Slide

  21. 4. Common patterns
    Transforming patterns:
    - Map
    - FlatMap
    - GroupBy
    - Buffer

    View Slide

  22. 4. Common patterns
    Filtering patterns:
    - Filter
    - Debounce
    - First
    - Last

    View Slide

  23. 4. Common patterns
    Combine patterns:
    - Concat
    - Join
    - Merge
    - Zip

    View Slide

  24. 5. Key takeaways
    1. Reactive Programming is paradigm based on async stream processing with
    the support of functional programming.
    2. Realize with ReactiveX language implementations: RxJava, RxKotlin,
    RxSwift, RxJS
    3. Rx provided with set of common tools/pattern to solve data manipulating
    in an asynchronous manner.

    View Slide

  25. Thank you

    View Slide