/CoroutinesRx

A display for battle of Asynchronousity between Coroutines and Rx

Primary LanguageKotlin

CoroutinesRx

A display for battle of Asynchronousity between Coroutines and Rx. This repository consist of how the reactive world of RxJava looks like in Coroutines world.

SpeakerDeck Link: https://speakerdeck.com/amanjeetsingh150/coroutines-and-rx-a-battle-towards-asynchronicity

Battles

  • Exception Handling

    Shows standard way of handling errors in RxJava and coroutines here in ExceptionViewModel. The standard ways of handling errors in Rx and coroutines are as follows here:
    • RxJava: RxJava handles exception with variety of operators here.
    • Coroutines: Coroutines exception handling depends on type of coroutine builder. Flows have its operator to catch errors on the chain.
  • Zipping Results

    Shows how 2 API calls will be zipped in RxJava and coroutines. Rx has its own zip operator and coroutines needs implementing the own zip operator. We have to customize the operator to make it scalable to zip various calls:
    • RxJava: RxJava has its own zip operator and we can zip api calls with it using 2 observable and the BiFunction as shown here.
    • Coroutines: Coroutines implementation of zip has been done here in Extensions file.
    • Flows: Flows can also be used to zip api calls. As shown here it has variety of operators available.
  • State Management

    • RxJava: Using RxJava we can acheive Reactive and functional paradigm which can easily offer you architecure to stream out UI events and different stream based events which helps you to reduce a state for your program. It is rich with operators, lifecycle management is done very easily and thread switching is also easy. You can have a look at example here.
    • Coroutines: Coroutines were having a gap of missing a reactive and composable part which is now fulfilled by the Kotlin Flows API. It is representation of cold streams and can be simply thought upon as suspension based reactive streams. Example with flows is given here.
  • Performace comparison

    The performance comparison is done by performing same kind of operations with coroutines and Rx both. By using 2 different number of data set first with 10,000 iterations and then with 100,000 iterations. The operation is analogy of n number of data set on which you do some operations like flatmapping the result to get shows result and further adding to list. The change in memory is shown belown in both case. The operation on the data with Rx can be seen here and coroutines is here:

    10,000 iterations
    Base Memory Max change Delta change
    Rx 72.6 MB 93.2 MB 20.6 MB
    Coroutines 64.6 MB 83.4 MB 18.8 MB
    RxJava:

    Coroutines:


  • 100,000 iterations
    Base Memory Max change Delta change
    Rx 65.5 MB 97.7 MB 32.2 MB
    Coroutines 65 MB 89 MB 24 MB
    RxJava:

    Coroutines:
  • Future tasks:

    • The substitution of callbacks.
    • The ui bindings
    • Proper documentation

Contribution

You are welcome to open PRs / issues, Give suggestions and feedback. This repository consist of the example of substitutions of coroutines in Rx worlds. So, write a short description on PR for that.