Coroutines and RxJava an Asynchronicity Comparison (Part 5): Operators

IntroductionIn this blog series I will compare Kotlin Coroutines and RxJava since they are both trying to solve a common problem in Android development: Asynchronous Programming.In Part 3 and Part 4 we talked about transferring stream of values and the Coroutines and RxJava interop library.Now its time to compare how we can transform those streams with Operators.

Common OperatorsSome RxJava operators are available in the Kotlin standard library as part of Kotlin Collections. Here you can see a table comparing simple operators.These operators transform the stream in the same way even though some operators differ in name: e.

g. skip in RxJava is called drop in Coroutines.Create Your Own OperatorSome operators are not part of Kotlin Collections. However, you can create them yourself with little effort.

We can replicate what the range RxJava operator does with few lines of code and a simple for loop.fun range( context: CoroutineContext, start: Int, count: Int) publish(context) for (x in start until start count) send(x)Some other operators require more work. In the example below you can see the implementation of the Completable.

zip RxJava operator which takes two blocks of code and waits for both of them to finish.suspend fun zip( context: CoroutineContext, block: () -> Unit, block2: () -> Unit) val deferred1 async(context) block() val deferred2 async(context) block2() deferred1.await() deferred2.await()If you noticed, we pass a CoroutineContext as a parameter. We do that so we can cancel the operator easily by calling .cancel() on that contexts Job as we talked about in Part 2.Complex OperatorsWhat about even more complex RxJava operators like debounce?You can find debounce as an extension function on ReceiveChannel. RxJava timeout has an equivalent in Kotlin Coroutines with withTimeoutOrNull, etc.Similarities and DifferencesWe see that most operators are available in both libraries and if not, you can easily build them.The only difference I can see in these two libraries is at which point you apply those operatorsWhereas in RxJava you can apply operators before subscribing to the stream, you have to do it after opening a subscription in Coroutines. Lets see how we map an element in RxJava:rxObservable .map result -> map(result) .subscribe( consumeResult(it) )And now how we do it in Coroutines:broadcastChannel .openSubscription() .map result -> map(result) In Coroutines we have to do it after opening a subscription because map in Coroutines is an extension function on ReceiveChannel. This is the case for other operators such as filter, drop, etc. Calling openSubscription() returns a SubscriptionReceiveChannel object which extends ReceiveChannel.In my opinion, Coroutines requires extra work if you want multiple Observers to apply the same operators. Of course you can do it! But it requires more code.Whats Coming Next?Threading is the topic for the sixth part of this series.If you want to know how you can swap threading in Coroutines the same way you do it in RxJava with subscribeOn() and observeOn(), dont miss next weeks blog post!More educationMore articles about RxJavaRxJava: Android MVVM App structure with RetrofitIntroductionmedium.

comRxJava 2: Android MVVM Lifecycle App Structure with Retrofit 2Its been more than two years since I wrote about MVVM, RxJava and Retrofit. Lets do an update.medium.comThanks for reading,Manuel Vicente Vivo

hot searches
Handmade foldable magnetic gift box Private Label 3ml Blue Gigival Barrier Gel Gum Protector Protection Gel No Sensitive For Teeth Whitening Rechargeable teethwhitening kit with 16/24/32leds cold blue light private design in box leds and pens'logo Private label teeth whitening activated charcoal powder 30g 40g 60g 100g organic natural coconut charcoal powder for daily use CE Approved Advanced Smart Home Use Dental Bleaching System Rechargeable Led Light Teeth Whitening Kit Private Best Selling GlorySmile Teeth Bleaching System 2ml Gel Pen New Rechargeable Teeth Whitening Light Kit With Private Logo OEM Factory Bamboo Natural Activated Charcoal Dental Toothpaste Teeth Whitening Charcoal Toothpaste 100% organic activated coal teeth whitening charcoal powder for teeth whitening Home Use Bright Your Smile dry strip Teeth Whitening Mint Flavor 16%HP Peroxide dry Strips Custom Luxury Custom Eyelash Packaging Empty Eyelash Packaging Box
hot articles
Cooperation with Japanese pioneer
194
Tianci acquires HEIDELBERG speedmaster
193
Shenzhen Tianci printing&packaging CO., LTD Printing&Packaging ltd in Print China 2019
164
Shenzhen Tianci printing&packaging CO., LTD Printing&Packaging ltd in Print China 2020
158
Q:What kind of file format should I provide?
140
Q:Do you have graphic designer that can help us with design?
132
Q:How long has your company been established?
123
Q:Can i get a free samples?
120
How about Tianci independent R&D capabilities?
117
How about the ODM service flow?
112
onetech related articles
What Are Some Ways to Keep My Hair Really Healthy?
How Was the First Sale of Vivo X60 Series?
Huami Technology Held a Press Conference on August 27, and Amazfit Smart Sports Watch 3 Officially A
Connect to Nonencrypted Wireless Network Using Ubuntu Commands
Is Time Travel Possible? Can We Travel Back in Time?

Copyright © 2020 Coffee bag - Guangzhou tianci packaging industry Co,. Ltd. | Sitemap