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

Breaking up with RxJava (Kotlin Coroutines & Channels)

Breaking up with RxJava (Kotlin Coroutines & Channels)

Held on 26th June 2018 Kotlin Zagreb Meetup:
https://www.meetup.com/Kotlin-Zagreb/events/251748671/

When RxJava came along, it made our lives so easy that most Android developers jumped ship.
Soon, it was everywhere and everything was a stream. But was that really a good idea?
Now that we have Kotlin, a lot of painpoints solved by (ab)using RxJava are solvable by using plain Kotlin (well, experimental) coroutines.
So should we be jumping ship again? What makes Kotlin a better solution for most of your problems?
In this talk we’ll take a dive into some of Kotlin’s coroutine features and find out how, when and why to say “No” to RxJava.

Ian Rumac

June 28, 2018
Tweet

More Decks by Ian Rumac

Other Decks in Programming

Transcript

  1. View Slide

  2. View Slide

  3. View Slide

  4. Breaking up with Rx

    View Slide

  5. Breaking up with Rx
    Ian Rumac
    Software Architect @ Undabot

    View Slide

  6. The story so far

    View Slide

  7. What RxJava gives us:

    The Good Parts

    View Slide

  8. What RxJava gives us:

    The Good Parts
    • Easy stream manipulation
    • Simple concurrency
    • Chained operators
    • Observing state changes
    • Combining operators

    View Slide

  9. What RxJava gives us:

    The Good Parts
    • Easy stream manipulation
    • Simple concurrency
    • Chained operators
    • Observing state changes
    • Combining operators

    View Slide

  10. And we subscribed({});

    View Slide

  11. And all was good.

    View Slide

  12. View Slide

  13. View Slide

  14. View Slide

  15. View Slide

  16. View Slide

  17. View Slide

  18. View Slide

  19. What RxJava gives us:

    The Bad Parts

    View Slide

  20. • One library that spans across all layers

    (remember migrating?)
    • One library which you shape your code around
    • One paradigm you force everything into
    • One naming style
    • One extra layer of complexity
    • Code
    • Mental
    What RxJava gives us:

    The Bad Parts

    View Slide

  21. • One library that spans across all layers

    (remember migrating?)
    • One library which you shape your code around
    • One paradigm you force everything into
    • One naming style
    • One extra layer of complexity
    • Code
    • Mental
    What RxJava gives us:

    The Bad Parts

    View Slide

  22. The core

    View Slide

  23. • Is your business logic wrapped in Rx?
    • Is your business logic a stream? 

    (not a stream of one )
    • Why do you represent the core logic of your app
    with a stream if it isn’t one?
    The core

    View Slide

  24. So, what can we do about it?

    View Slide

  25. Replacing the operator magic

    View Slide

  26. Replacing the operator magic
    • Collection extensions already in stdlib

    View Slide

  27. Replacing the operator magic
    • Collection extensions already in stdlib

    View Slide

  28. Replacing the operator magic
    • Collection extensions already in stdlib

    View Slide

  29. Replacing the operator magic
    • Collection extensions already in stdlib
    • Easy to write your own

    View Slide

  30. Replacing the operator magic
    • Collection extensions already in stdlib
    • Easy to write your own

    View Slide

  31. Replacing the operator magic
    • Collection extensions already in stdlib
    • Easy to write your own

    View Slide

  32. Replacing the operator magic
    • Collection extensions already in stdlib
    • Easy to write your own
    • Receiver lambdas

    View Slide

  33. Replacing the operator magic
    • Collection extensions already in stdlib
    • Easy to write your own
    • Receiver lambdas

    View Slide

  34. Replacing the operator magic
    • Collection extensions already in stdlib
    • Easy to write your own
    • Receiver lambdas

    View Slide

  35. Replacing the operator magic
    • Collection extensions already in stdlib
    • Easy to write your own
    • Receiver lambdas

    View Slide

  36. Observing state changes

    View Slide

  37. Observing state changes
    • Observable delegate

    View Slide

  38. Observing state changes
    • Observable delegate

    View Slide

  39. Observing state changes
    • Observable delegate
    • Vetoable delegate

    View Slide

  40. Observing state changes
    • Observable delegate
    • Vetoable delegate

    View Slide

  41. Observing state changes
    • Observable delegate
    • Vetoable delegate

    View Slide

  42. Observing state changes
    • Observable delegate
    • Vetoable delegate

    View Slide

  43. Observing state changes
    • Observable delegate
    • Vetoable delegate
    • Simple

    View Slide

  44. Observing state changes
    • Observable delegate
    • Vetoable delegate
    • Simple
    • Cover a lot of usecases

    View Slide

  45. Observing state changes
    • Observable delegate
    • Vetoable delegate
    • Simple
    • Cover a lot of usecases

    View Slide

  46. Observing state changes
    • Observable delegate
    • Vetoable delegate
    • Simple
    • Cover a lot of usecases

    View Slide

  47. Coroutines

    View Slide

  48. Coroutines
    • Language feature
    • Lightweight
    • Context(s)
    • Easy thread switching

    View Slide

  49. Coroutines

    View Slide

  50. So just tldr light threads?

    View Slide

  51. Coroutines

    View Slide

  52. Coroutines
    • Easy concurrency
    • Yield!
    • Nesting and composing
    • cancelChildren()

    View Slide

  53. Composing & Combining
    • Easy
    • Human readable
    • Wrap it any way you want

    View Slide

  54. View Slide

  55. doOne
    doTwo

    View Slide

  56. View Slide

  57. doOne doTwo

    View Slide

  58. View Slide

  59. doOne doTwo

    View Slide

  60. View Slide

  61. View Slide

  62. View Slide

  63. View Slide

  64. But wait, there’s more!

    View Slide

  65. Channels 101

    View Slide

  66. Channels 101
    • A hot asynchronous stream of elements

    View Slide

  67. Channels 101
    • A hot asynchronous stream of elements
    • Rendezvous transfer

    View Slide

  68. Channels 101
    • A hot asynchronous stream of elements
    • Rendezvous transfer
    • Suspending

    View Slide

  69. Channels 101
    • A hot asynchronous stream of elements
    • Rendezvous transfer
    • Suspending
    • Once it’s consumed, it’s consumed

    View Slide

  70. Channels 101
    • A hot asynchronous stream of elements
    • Rendezvous transfer
    • Suspending
    • Once it’s consumed, it’s consumed
    • 1 item at a time

    View Slide

  71. Channels 101

    View Slide

  72. Channels 101

    View Slide

  73. Channels 101

    View Slide

  74. Channels 101

    View Slide

  75. Channels 101

    View Slide

  76. Channels 101

    View Slide

  77. Channels of all shapes and sizes!
    • ArrayChannel - Buffered!
    • ConflatedChannel - Only the latest element
    • Ticker channel - Time based operations
    • BroadcastChannel & ArrayBroadcastChannel
    “Broadcast channel is a non-blocking primitive for communication between the sender and multiple receivers
    that subscribe for the elements using [open] function and unsubscribe using [SubscriptionReceiveChannel.close]
    function.”
    ReceiveChannel

    View Slide

  78. View Slide

  79. … well I still need my operators…

    View Slide

  80. … well I still need my operators…

    View Slide

  81. … well I still need my operators…

    View Slide

  82. … well I still need my operators…

    View Slide

  83. … well I still need my operators…

    View Slide

  84. … well I still need my operators…

    View Slide

  85. … well I still need my operators…

    View Slide

  86. … well I still need my operators…

    View Slide

  87. View Slide

  88. …that’s all nice, but how do I test it?

    View Slide

  89. …that’s all nice, but how do I test it?
    Just like any other code.
    (Context = Unconfined)

    View Slide

  90. Thinking about dumping your old library?

    View Slide

  91. Thinking about dumping your old library?
    • KotlinX Coroutines Rx 1&2
    • Kotlin Coroutine (Experimental) Retrofit Adapter
    • Still experimental!
    • Wrap the boilerplate

    View Slide

  92. Conclusion

    View Slide

  93. Conclusion
    Kotlin > RxJava

    View Slide

  94. Conclusion

    View Slide

  95. Conclusion
    Q & A?

    View Slide

  96. View Slide

  97. Thank you for your time!
    Ian Rumac
    Software Architect @Undabot

    View Slide