{"id":255,"date":"2020-08-13T11:50:00","date_gmt":"2020-08-13T09:50:00","guid":{"rendered":"https:\/\/www.codemotion.com\/magazine\/async-code-on-kotlin-coroutines-vs-rxjava\/"},"modified":"2020-09-01T16:28:15","modified_gmt":"2020-09-01T14:28:15","slug":"async-code-on-kotlin-coroutines-vs-rxjava","status":"publish","type":"post","link":"https:\/\/www.codemotion.com\/magazine\/frontend\/mobile-dev\/async-code-on-kotlin-coroutines-vs-rxjava\/","title":{"rendered":"Async code on Kotlin: coroutines VS RxJava"},"content":{"rendered":"\t\t\t\t<div class=\"wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-5d4f8daf      \"\n\t\t\t\t\tdata-scroll= \"1\"\n\t\t\t\t\tdata-offset= \"30\"\n\t\t\t\t\tstyle=\"\"\n\t\t\t\t>\n\t\t\t\t<div class=\"uagb-toc__wrap\">\n\t\t\t\t\t\t<div class=\"uagb-toc__title\">\n\t\t\t\t\t\t\tTable Of Contents\t\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<div class=\"uagb-toc__list-wrap \">\n\t\t\t\t\t\t<ol class=\"uagb-toc__list\"><li class=\"uagb-toc__list\"><a href=\"#what-are-coroutines\" class=\"uagb-toc-link__trigger\">What are coroutines?<\/a><li class=\"uagb-toc__list\"><a href=\"#what-is-rxjava\" class=\"uagb-toc-link__trigger\">What is RxJava?<\/a><li class=\"uagb-toc__list\"><a href=\"#a-sample-service-in-kotlin\" class=\"uagb-toc-link__trigger\">A sample service in Kotlin<\/a><li class=\"uagb-toc__list\"><a href=\"#using-service-methods\" class=\"uagb-toc-link__trigger\">Using service methods<\/a><li class=\"uagb-toc__list\"><a href=\"#coroutines-or-rxjava\" class=\"uagb-toc-link__trigger\">Coroutines or RxJava?<\/a><li class=\"uagb-toc__list\"><a href=\"#conclusions\" class=\"uagb-toc-link__trigger\">Conclusions<\/a><\/ol>\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\n\n\n<p><span class=\"firstcharacter\">A<\/span> lot of <a href=\"https:\/\/www.codemotion.com\/magazine\/tag\/android\/\">Android<\/a> programmers are now exploring <strong><span id=\"urn:enhancement-4879c278\" class=\"textannotation wl-other disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/kotlin_programming_language\">Kotlin<\/span><\/strong>, a <span id=\"urn:enhancement-477ff851\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/programming_languages\">programming language<\/span> based on <span id=\"urn:enhancement-96a18bc8\" class=\"textannotation disambiguated wl-creative-work\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/java_software_platform\">Java<\/span> and the <span id=\"urn:enhancement-d4a7e074\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/java_virtual_machine\">JVM<\/span> that is used to develop <span id=\"urn:enhancement-8d4e7112\" class=\"textannotation disambiguated wl-creative-work\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/android_operating_system\">Android<\/span> applications. As is often the case with Java-based <span id=\"urn:enhancement-c93b33d2\" class=\"textannotation disambiguated wl-creative-work\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/android_operating_system\">Android<\/span> <span id=\"urn:enhancement-257f1bd0\" class=\"textannotation wl-other disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/mobile_app\">apps<\/span>, one of the main problems when <a href=\"https:\/\/www.codemotion.com\/magazine\/dev-hub\/community-manager\/interview-leonardo-pirro-getting-started-with-kotlin-on-android\/\">approaching <span id=\"urn:enhancement-6a62eacf\" class=\"textannotation disambiguated wl-creative-work\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/android_operating_system\">Android<\/span> development in <span id=\"urn:enhancement-a1b2f828\" class=\"textannotation wl-other disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/kotlin_programming_language\">Kotlin<\/span><\/a> is the <span id=\"urn:enhancement-97477a93\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/management\">management<\/span> of <strong><span id=\"urn:enhancement-d829af67\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/asynchrony\">asynchronous<\/span> code<\/strong>. In <span id=\"urn:enhancement-cc3415c2\" class=\"textannotation disambiguated wl-creative-work\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/java_software_platform\">Java<\/span>, this has been solved by the <code>AsyncTask<\/code> <span id=\"urn:enhancement-7e76de66\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/class_computer_programming\">class<\/span>, or the <code>runOnUiThread<\/code> method. In <span id=\"urn:enhancement-cf2c46ab\" class=\"textannotation wl-other disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/kotlin_programming_language\">Kotlin<\/span>, something different is required.<\/p>\n\n\n\n<p>At Codemotion Milan 2018, <a href=\"https:\/\/milan2018.codemotionworld.com\/speaker\/306\/\" target=\"_blank\" rel=\"noopener noreferrer\">Fabio Collini<\/a>, <span id=\"urn:enhancement-f7cd3f64\" class=\"textannotation disambiguated wl-organisation\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/google\">Google<\/span> Developer Expert and Senior <span id=\"urn:enhancement-9b4236d0\" class=\"textannotation disambiguated wl-creative-work\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/android_operating_system\">Android<\/span> Developer, delivered a speech on how to implement <span id=\"urn:enhancement-b7a77098\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/asynchrony\">asynchronous<\/span> code in <span id=\"urn:enhancement-36df870f\" class=\"textannotation wl-other disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/kotlin_programming_language\">Kotlin<\/span>. Specifically, he described two different approaches: the first using <strong>coroutines<\/strong>, the second based on a library named <strong>ReactiveX Java<\/strong> (or <strong>RxJava<\/strong> in short). Both of these approaches allow users to implement and support <span id=\"urn:enhancement-26930518\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/asynchrony\">asynchronous<\/span> operations, and can be also used both together and interchangeably.<br>This post provides a quick overview on how these approaches differ from each other.<\/p>\n\n\n\n<p>Before starting, let&#8217;s try to understand more clearly the main differences between RxJava and coroutines, and how they can help Kotlin <span id=\"urn:enhancement-215c6bfe\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/software_developer_2\">programmers<\/span>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are coroutines?<\/h2>\n\n\n\n<p>The concept of coroutine is not strictly related to Kotlin. <span id=\"urn:enhancement-b959dcae\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/coroutine\">Coroutines<\/span> are in fact <span id=\"urn:enhancement-86d06a8e\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/component-based_software_engineering\">components<\/span> that generalize <span id=\"urn:enhancement-16625c56\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/subroutine\">subroutines<\/span> for non-<span id=\"urn:enhancement-8c2f17c4\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/preemption_computing\">preemptive<\/span> <span id=\"urn:enhancement-8058453b\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/computer_multitasking\">multitasking<\/span>, which allows tasks to suspend and resume their own execution. These <span id=\"urn:enhancement-9244a22b\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/component-based_software_engineering\">components<\/span> are therefore particularly useful for multi-thread programming, and are also found in other languages such as <span id=\"urn:enhancement-5f2b73f9\" class=\"textannotation disambiguated wl-creative-work\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/python_programming_language\">Python<\/span> or <span id=\"urn:enhancement-38cff192\" class=\"textannotation disambiguated wl-creative-work\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/c_sharp_programming_language\">C#<\/span>.<\/p>\n\n\n\n<p>In <span id=\"urn:enhancement-cd1395eb\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/kotlin_programming_language\">Kotlin<\/span>, coroutines are implemented within the <code>kotlinx.coroutines<\/code> library, originally <span id=\"urn:enhancement-b0d7bb14\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/software_developer\">developed<\/span> by JetBrains. The <span id=\"urn:enhancement-a0d3570c\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/kotlin_programming_language\">Kotlin<\/span> official documentation provides an extensive &#8216;how to&#8217; guide that is available at <a aria-label=\"undefined (opens in a new tab)\" href=\"https:\/\/kotlinlang.org\/docs\/reference\/coroutines\/coroutines-guide.html\" target=\"_blank\" rel=\"noreferrer noopener\">this link<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is RxJava?<\/h2>\n\n\n\n<p>RxJava, shorthand for &#8216;ReactiveX for <span id=\"urn:enhancement-f52188ff\" class=\"textannotation disambiguated wl-creative-work\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/java_software_platform\">Java<\/span>&#8216;, was created by Ben Christensen and David Karnok, with the first <span id=\"urn:enhancement-f0edb7a6\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/software_versioning\">version<\/span> released in November 2014. RxJava has found several <span id=\"urn:enhancement-e91e8f9a\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/application_software\">applications<\/span> in the Android world, thanks to its unique features. Indeed, this library allows easy termination of subscription when an activity is ending, thus avoiding memory leaks and the waste of <span id=\"urn:enhancement-f0102af7\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/resource\">resources<\/span> derived from working on UI elements that are no longer visible.<\/p>\n\n\n\n<p>Moreover, RxJava is considered one of the most fruitful methods for enabling <strong><span id=\"urn:local-annotation-49891\" class=\"textannotation disambiguated\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/reactive_programming\">Reactive<\/span> <span id=\"urn:enhancement-abc4c981\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/computer_programming\">Programming<\/span><\/strong> in <span id=\"urn:enhancement-7b47ec62\" class=\"textannotation disambiguated wl-creative-work\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/android_operating_system\">Android<\/span> development. As we&#8217;ll see below, the result is simplified <span id=\"urn:enhancement-ec59458a\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/implementation\">implementation<\/span> of <span id=\"urn:enhancement-a80f839b\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/concurrency_computer_science\">concurrency<\/span> and the asynchronous tasks inherent in <span id=\"urn:enhancement-94b3975d\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/mobile_application_development\">mobile programming<\/span>.<\/p>\n\n\n\n<p>Although it is also available for <span id=\"urn:enhancement-f87f57b9\" class=\"textannotation disambiguated wl-creative-work\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/java_software_platform\">Java<\/span>,this article will focus on how to use RxJava to implement mobile <span id=\"urn:enhancement-b5d5d003\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/mobile_app\">apps<\/span> in <span id=\"urn:enhancement-138ead47\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/kotlin_programming_language\">Kotlin<\/span>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">A sample service in Kotlin<\/h2>\n\n\n\n<p>Fabio Collini started the discussion about <span id=\"urn:enhancement-dc721b78\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/mobile_application_development\">using<\/span> RxJava or coroutines by showing a sample interface for a service aimed at obtaining information from <span id=\"urn:enhancement-1eff9ac4\" class=\"textannotation disambiguated wl-creative-work\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/stack_overflow\">StackOverflow<\/span>. Since such a service is made up of <span id=\"urn:enhancement-683a362a\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/asynchrony\">asynchronous<\/span> <span id=\"urn:enhancement-957a3296\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/method_computer_programming\">methods<\/span>, it is perfect for our purposes.<\/p>\n\n\n\n<p>The first question is therefore how to define <span id=\"urn:enhancement-90017e51\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/method_computer_programming\">methods<\/span> that are <span id=\"urn:enhancement-37ee33fb\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/asynchrony\">asynchronous<\/span>? With RxJava, we need to use a <code>Single<\/code> <span id=\"urn:enhancement-a71549a9\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/class_computer_programming\">class<\/span> as type for the return value:<\/p>\n\n\n\n<div style=\"background: #ffffff; overflow: auto; width: auto;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">interface<\/span> StackOverflowService {\n\t@<span id=\"urn:enhancement-82ae2acf\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/hypertext_transfer_protocol\">GET<\/span>(<span style=\"background-color: #fff0f0;\">\"\/<span id=\"urn:enhancement-f6a54c32\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/user_computing\">users<\/span>\"<\/span>)\n\t<span style=\"color: #008800; font-weight: bold;\">fun<\/span> <span style=\"color: #0066bb; font-weight: bold;\">getTopUsers<\/span>(): Single&lt;List&lt;<span id=\"urn:enhancement-751b77ef\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/user_computing\">User<\/span>&gt;&gt;\n    \n\t@<span id=\"urn:enhancement-faa0d6f8\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/hypertext_transfer_protocol\">GET<\/span>(<span style=\"background-color: #fff0f0;\">\"\/<span id=\"urn:enhancement-3261eb38\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/user_computing\">users<\/span>\/{userId}\/badges\"<\/span>)\n\t<span style=\"color: #008800; font-weight: bold;\">fun<\/span> <span style=\"color: #0066bb; font-weight: bold;\">getBadges<\/span>(\n    \t\t@Path(<span style=\"background-color: #fff0f0;\">\"userId\"<\/span>) userId: <span id=\"urn:enhancement-2f54eb95\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/integer_computer_science\">Int<\/span>\n\t): Single&lt;List&lt;Badge&gt;&gt;\n    \n\t@<span id=\"urn:enhancement-53503045\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/hypertext_transfer_protocol\">GET<\/span>(<span style=\"background-color: #fff0f0;\">\"\/<span id=\"urn:enhancement-d5b0f02\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/user_computing\">users<\/span>\/{userId}\/top-tags\"<\/span>)\n\t<span style=\"color: #008800; font-weight: bold;\">fun<\/span> <span style=\"color: #0066bb; font-weight: bold;\">getTags<\/span>(\n   \t\t@Path(<span style=\"background-color: #fff0f0;\">\"userId\"<\/span>) userId: <span id=\"urn:enhancement-541d91d9\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/integer_computer_science\">Int<\/span>\n\t): Single&lt;List&lt;Tag&gt;&gt;\n}\n<\/pre>\n<\/div>\n\n\n\n<p>This allows use of the <code>subscribe<\/code> <span id=\"urn:enhancement-5883526e\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/method_computer_programming\">method<\/span> in order to wait for the response.<br>With coroutines, there are two possibilities: the first is similar to the above, but uses the <code>Deferred<\/code> <span id=\"urn:enhancement-77a93bf4\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/class_computer_programming\">class<\/span>:<\/p>\n\n\n\n<div style=\"background: #ffffff; overflow: auto; width: auto;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">interface<\/span> StackOverflowService {\n\t@<span id=\"urn:enhancement-c25ff3dc\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/hypertext_transfer_protocol\">GET<\/span>(<span style=\"background-color: #fff0f0;\">\"\/<span id=\"urn:enhancement-e39f813\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/user_computing\">users<\/span>\"<\/span>)\n\t<span style=\"color: #008800; font-weight: bold;\">fun<\/span> <span style=\"color: #0066bb; font-weight: bold;\">getTopUsers<\/span>(): Deferred&lt;List&lt;<span id=\"urn:enhancement-76becc59\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/user_computing\">User<\/span>&gt;&gt;\n    \n\t@<span id=\"urn:enhancement-98fa99e3\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/hypertext_transfer_protocol\">GET<\/span>(<span style=\"background-color: #fff0f0;\">\"\/<span id=\"urn:enhancement-ad4cbec8\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/user_computing\">users<\/span>\/{userId}\/badges\"<\/span>)\n\t<span style=\"color: #008800; font-weight: bold;\">fun<\/span> <span style=\"color: #0066bb; font-weight: bold;\">getBadges<\/span>(\n    \t\t@Path(<span style=\"background-color: #fff0f0;\">\"userId\"<\/span>) userId: <span id=\"urn:enhancement-2725c4a5\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/integer_computer_science\">Int<\/span>\n\t): Deferred&lt;List&lt;Badge&gt;&gt;\n    \n\t@<span id=\"urn:enhancement-9e6b5c12\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/hypertext_transfer_protocol\">GET<\/span>(<span style=\"background-color: #fff0f0;\">\"\/<span id=\"urn:enhancement-ea15afa5\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/user_computing\">users<\/span>\/{userId}\/top-tags\"<\/span>)\n\t<span style=\"color: #008800; font-weight: bold;\">fun<\/span> <span style=\"color: #0066bb; font-weight: bold;\">getTags<\/span>(\n   \t\t@Path(<span style=\"background-color: #fff0f0;\">\"userId\"<\/span>) userId: <span id=\"urn:enhancement-abf9a8cd\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/integer_computer_science\">Int<\/span>\n\t): Deferred&lt;List&lt;Tag&gt;&gt;\n}\n<\/pre>\n<\/div>\n\n\n\n<p>However, a recent upgrade of <span id=\"urn:enhancement-c0d04417\" class=\"textannotation wl-other disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/kotlin_programming_language\">Kotlin<\/span> allows for declaration of <strong>suspending <span id=\"urn:enhancement-5e52b255\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/subroutine\">functions<\/span><\/strong>, which in practice makes the code appears synchronous, even it is actually the opposite:<\/p>\n\n\n\n<div style=\"background: #ffffff; overflow: auto; width: auto;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">interface<\/span> StackOverflowService {\n\t@<span id=\"urn:enhancement-e52af9d5\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/hypertext_transfer_protocol\">GET<\/span>(<span style=\"background-color: #fff0f0;\">\"\/<span id=\"urn:enhancement-c1a9c723\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/user_computing\">users<\/span>\"<\/span>)\n\tsuspend <span style=\"color: #008800; font-weight: bold;\">fun<\/span> <span style=\"color: #0066bb; font-weight: bold;\">getTopUsers<\/span>(): List&lt;<span id=\"urn:enhancement-e9b11f10\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/user_computing\">User<\/span>&gt;\n    \n\t@<span id=\"urn:enhancement-5e0b8392\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/hypertext_transfer_protocol\">GET<\/span>(<span style=\"background-color: #fff0f0;\">\"\/<span id=\"urn:enhancement-81deaff1\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/user_computing\">users<\/span>\/{userId}\/badges\"<\/span>)\n\tsuspend <span style=\"color: #008800; font-weight: bold;\">fun<\/span> <span style=\"color: #0066bb; font-weight: bold;\">getBadges<\/span>(\n    \t\t@Path(<span style=\"background-color: #fff0f0;\">\"userId\"<\/span>) userId: <span id=\"urn:enhancement-ea67092e\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/integer_computer_science\">Int<\/span>\n\t): List&lt;Badge&gt;\n    \n\t@<span id=\"urn:enhancement-6500960d\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/hypertext_transfer_protocol\">GET<\/span>(<span style=\"background-color: #fff0f0;\">\"\/<span id=\"urn:enhancement-7f495090\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/user_computing\">users<\/span>\/{userId}\/top-tags\"<\/span>)\n\tsuspend <span style=\"color: #008800; font-weight: bold;\">fun<\/span> <span style=\"color: #0066bb; font-weight: bold;\">getTags<\/span>(\n   \t\t@Path(<span style=\"background-color: #fff0f0;\">\"userId\"<\/span>) userId: <span id=\"urn:enhancement-643e447d\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/integer_computer_science\">Int<\/span>\n\t): List&lt;Tag&gt;\n}\n<\/pre>\n<\/div>\n\n\n\n<p>In what follows, we will consider the latter approach, which makes the code cleaner.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using service methods<\/h2>\n\n\n\n<p>Now, if we want to use the aforementioned service, <span id=\"urn:local-annotation-521620\" class=\"textannotation disambiguated\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/rxjava\">RxJava<\/span> and coroutines require different approaches. As mentioned above, RxJava requires using the <code>subscribe<\/code> <span id=\"urn:enhancement-fcd43bf4\" class=\"textannotation wl-other disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/method_computer_programming\">method<\/span>:<\/p>\n\n\n\n<div style=\"background: #ffffff; overflow: auto; width: auto;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\"><span id=\"urn:enhancement-c01d5cc8\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/class_computer_programming\">class<\/span><\/span> <span style=\"color: #0066bb; font-weight: bold;\">MyViewModel<\/span>(\n\t<span style=\"color: #008800; font-weight: bold;\">private<\/span> <span style=\"color: #008800; font-weight: bold;\">val<\/span> service: StackOverflowService\n) : ViewModel() {\n\n\t<span style=\"color: #008800; font-weight: bold;\">private<\/span> <span style=\"color: #008800; font-weight: bold;\">val<\/span> disposable = CompositeDisposable()\n\n\t<span style=\"color: #008800; font-weight: bold;\">fun<\/span> <span style=\"color: #0066bb; font-weight: bold;\">load<\/span>() {\n\t\tdisposable +=\n\t\t\tservice.getTopUsers()\n\t\t\t\t.subscribeOn(io())\n\t\t\t\t.observeOn(mainThread())\n\t\t\t\t.subscribe(\n\t\t\t\t\t{ <span id=\"urn:enhancement-1e7b86fa\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/user_computing\">users<\/span> -&gt; updateUi(<span id=\"urn:enhancement-2a40539c\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/user_computing\">users<\/span>) },\n\t\t\t\t\t{ e -&gt; updateUi(e) }\n\t\t\t\t)\n\t}\n    \n\t<span style=\"color: #008800; font-weight: bold;\">private<\/span> <span style=\"color: #008800; font-weight: bold;\">fun<\/span> <span style=\"color: #0066bb; font-weight: bold;\">updateUi<\/span>(s: Any) {\n\t\t<span style=\"color: #888888;\">\/\/...<\/span>\n\t}\n    \n\t<span style=\"color: #008800; font-weight: bold;\">override<\/span> <span style=\"color: #008800; font-weight: bold;\">fun<\/span> <span style=\"color: #0066bb; font-weight: bold;\">onCleared<\/span>() {\n\t\tdisposable.clear()\n\t}\n}\n<\/pre>\n<\/div>\n\n\n\n<p>The subscribe <span id=\"urn:enhancement-4a42c190\" class=\"textannotation wl-other disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/method_computer_programming\">method<\/span> accepts two parameters: the first is the one we want to execute, while the second (<code>e -&gt; updateUi(e)<\/code>) is used in case of errors.<\/p>\n\n\n\n<p>While RxJava is synchronous by default (one consequence of this is that we resorted to subscribe), using coroutines and suspending <span id=\"urn:enhancement-fc766f18\" class=\"textannotation wl-other disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/subroutine\">functions<\/span> allows us to write <span id=\"urn:local-annotation-931654\" class=\"textannotation disambiguated\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/source_code\">code<\/span> that is automatically interpreted as <span id=\"urn:enhancement-50bd81c4\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/asynchrony\">asynchronous<\/span>, even if it appears synchronous. To understand this difference, compare the following snippet with the previous one: they are equivalent in functionality.<\/p>\n\n\n\n<div style=\"background: #ffffff; overflow: auto; width: auto;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\"><span id=\"urn:enhancement-3b95b9b6\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/class_computer_programming\">class<\/span><\/span> <span style=\"color: #0066bb; font-weight: bold;\">MyViewModel<\/span>(\n\t<span style=\"color: #008800; font-weight: bold;\">private<\/span> <span style=\"color: #008800; font-weight: bold;\">val<\/span> service: StackOverflowService\n) : ViewModel() {\n\n\t<span style=\"color: #008800; font-weight: bold;\">fun<\/span> <span style=\"color: #0066bb; font-weight: bold;\">load<\/span>() {\n\t\tviewModelScope.launch {\n\t\t\t<span style=\"color: #008800; font-weight: bold;\">try<\/span> {\n\t\t\t\t<span style=\"color: #008800; font-weight: bold;\">val<\/span> <span id=\"urn:enhancement-ec0115a0\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/user_computing\">users<\/span> = service.getTopUsers()\n\t\t\t\tupdateUi(<span id=\"urn:enhancement-7f979668\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/user_computing\">users<\/span>)\n\t\t\t} <span style=\"color: #008800; font-weight: bold;\">catch<\/span> (e: Exception) {\n\t\t\t\tupdateUi(e)\n\t\t\t}\n\t\t}\n\t}\n    \n\t<span style=\"color: #008800; font-weight: bold;\">private<\/span> <span style=\"color: #008800; font-weight: bold;\">fun<\/span> <span style=\"color: #0066bb; font-weight: bold;\">updateUi<\/span>(s: Any) {\n\t\t<span style=\"color: #888888;\">\/\/...<\/span>\n\t}\n}\n<\/pre>\n<\/div>\n\n\n\n<p>Here, <code>viewModelScope<\/code> simplifies the code, even if it is not available yet using <span id=\"urn:enhancement-e10c4ffe\" class=\"textannotation wl-other disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/kotlin_programming_language\">Kotlin<\/span>. However, an <span id=\"urn:enhancement-a6415345\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/implementation\">implementation<\/span> is provided in <a href=\"https:\/\/android-review.googlesource.com\/c\/platform\/frameworks\/support\/+\/772083\" target=\"_blank\" rel=\"noopener noreferrer\">this link<\/a>.<\/p>\n\n\n\n<p>In general, even more complicated things are made possible by using coroutines or RxJava. Additional code examples by Fabio Collini are <a href=\"https:\/\/github.com\/fabioCollini\/RxJavaVsCoroutines\" target=\"_blank\" rel=\"noopener noreferrer\">available on <span id=\"urn:local-annotation-802439\" class=\"textannotation disambiguated\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/github\">GitHub<\/span><\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Coroutines or RxJava?<\/h2>\n\n\n\n<p>Having read this quick overview of coroutines and RxJava, one might ask which solution is the best. In terms of number of <span id=\"urn:enhancement-3a1c4162\" class=\"textannotation wl-other disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/method_computer_programming\">methods<\/span>, RxJava is bigger than a solution based on coroutines only. However, this difference can be eliminated by using <strong>Proguard<\/strong> and its code optimization. The following graphs taken from Fabio Collini\u2019s slides show this.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/milan2018.codemotionworld.com\/speaker\/306\/\"><img decoding=\"async\" src=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/02\/method_count.png\" alt=\"\" class=\"wp-image-2474\"\/><\/a><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusions<\/h2>\n\n\n\n<p>So, which approach should we use for production code?<br>In order to answer such a question, Fabio Collini proposed the following &#8216;algorithm&#8217;:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>if you are already using RxJava and it works for you, then use <strong>RxJava<\/strong><\/li><li>if the architecture is based on a reactive stream, then use <strong>RxJava<\/strong><\/li><li>if the project is <span id=\"urn:enhancement-a21a704d\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/cross-platform\">multiplatform<\/span> with <span id=\"urn:enhancement-7be488c\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/kotlin_programming_language\">Kotlin<\/span> Native, then use <strong>coroutines<\/strong><\/li><li>if the <span id=\"urn:enhancement-1c3dfac5\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/codebase\">codebase<\/span> is <span id=\"urn:enhancement-4dffaf10\" class=\"textannotation disambiguated wl-creative-work\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/java_software_platform\">Java<\/span>\/<span id=\"urn:enhancement-a555d4e7\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/kotlin_programming_language\">Kotlin<\/span>, then use <strong>RxJava<\/strong><\/li><li>otherwise, use <strong>coroutines<\/strong><\/li><\/ul>\n\n\n\n<p>An additional note: consider that coroutines are generally easier to understand, while the <span id=\"urn:enhancement-2d008b55\" class=\"textannotation disambiguated wl-other\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/learning_curve\">learning curve<\/span> for RxJava is steep. This makes coroutines generally preferable, except in the aforementioned cases.<\/p>\n\n\n","protected":false},"excerpt":{"rendered":"<p>A lot of Android programmers are now exploring Kotlin, a programming language based on Java and the JVM that is used to develop Android applications. As is often the case with Java-based Android apps, one of the main problems when approaching Android development in Kotlin is the management of asynchronous code. In Java, this has&#8230; <a class=\"more-link\" href=\"https:\/\/www.codemotion.com\/magazine\/frontend\/mobile-dev\/async-code-on-kotlin-coroutines-vs-rxjava\/\">Read more<\/a><\/p>\n","protected":false},"author":3,"featured_media":7589,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_editorskit_title_hidden":false,"_editorskit_reading_time":4,"_editorskit_is_block_options_detached":false,"_editorskit_block_options_position":"{}","_uag_custom_page_level_css":"","_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[33],"tags":[22,55,4881],"collections":[],"class_list":{"0":"post-255","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-mobile-dev","8":"tag-codemotion-milan","9":"tag-kotlin","10":"tag-mobile","11":"entry"},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.9 (Yoast SEO v26.9) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Async code on Kotlin: coroutines VS RxJava - Codemotion Magazine<\/title>\n<meta name=\"description\" content=\"Two main approaches to implementing asynchronous code in Kotlin: the first using coroutines, the second based on the RxJava library.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.codemotion.com\/magazine\/frontend\/mobile-dev\/async-code-on-kotlin-coroutines-vs-rxjava\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Async code on Kotlin: coroutines VS RxJava\" \/>\n<meta property=\"og:description\" content=\"Two main approaches to implementing asynchronous code in Kotlin: the first using coroutines, the second based on the RxJava library.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.codemotion.com\/magazine\/frontend\/mobile-dev\/async-code-on-kotlin-coroutines-vs-rxjava\/\" \/>\n<meta property=\"og:site_name\" content=\"Codemotion Magazine\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/Codemotion.Italy\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-08-13T09:50:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-09-01T14:28:15+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/02\/zan-gxW3aq6onU-unsplash-scaled.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1012\" \/>\n\t<meta property=\"og:image:height\" content=\"675\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Vito Gentile\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:title\" content=\"Async code on Kotlin: coroutines VS RxJava\" \/>\n<meta name=\"twitter:creator\" content=\"@ViGentile\" \/>\n<meta name=\"twitter:site\" content=\"@CodemotionIT\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Vito Gentile\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/mobile-dev\/async-code-on-kotlin-coroutines-vs-rxjava\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/mobile-dev\/async-code-on-kotlin-coroutines-vs-rxjava\/\"},\"author\":{\"name\":\"Vito Gentile\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#\/schema\/person\/f9c99d8dfb99900d709bb8199225d313\"},\"headline\":\"Async code on Kotlin: coroutines VS RxJava\",\"datePublished\":\"2020-08-13T09:50:00+00:00\",\"dateModified\":\"2020-09-01T14:28:15+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/mobile-dev\/async-code-on-kotlin-coroutines-vs-rxjava\/\"},\"wordCount\":863,\"publisher\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/mobile-dev\/async-code-on-kotlin-coroutines-vs-rxjava\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/02\/zan-gxW3aq6onU-unsplash-scaled.jpg\",\"keywords\":[\"Codemotion Milan\",\"Kotlin\",\"Mobile\"],\"articleSection\":[\"Mobile Developer\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/mobile-dev\/async-code-on-kotlin-coroutines-vs-rxjava\/\",\"url\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/mobile-dev\/async-code-on-kotlin-coroutines-vs-rxjava\/\",\"name\":\"Async code on Kotlin: coroutines VS RxJava - Codemotion Magazine\",\"isPartOf\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/mobile-dev\/async-code-on-kotlin-coroutines-vs-rxjava\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/mobile-dev\/async-code-on-kotlin-coroutines-vs-rxjava\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/02\/zan-gxW3aq6onU-unsplash-scaled.jpg\",\"datePublished\":\"2020-08-13T09:50:00+00:00\",\"dateModified\":\"2020-09-01T14:28:15+00:00\",\"description\":\"Two main approaches to implementing asynchronous code in Kotlin: the first using coroutines, the second based on the RxJava library.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/mobile-dev\/async-code-on-kotlin-coroutines-vs-rxjava\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.codemotion.com\/magazine\/frontend\/mobile-dev\/async-code-on-kotlin-coroutines-vs-rxjava\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/mobile-dev\/async-code-on-kotlin-coroutines-vs-rxjava\/#primaryimage\",\"url\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/02\/zan-gxW3aq6onU-unsplash-scaled.jpg\",\"contentUrl\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/02\/zan-gxW3aq6onU-unsplash-scaled.jpg\",\"width\":1012,\"height\":675,\"caption\":\"mobile development developer\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/mobile-dev\/async-code-on-kotlin-coroutines-vs-rxjava\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.codemotion.com\/magazine\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Frontend\",\"item\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Mobile Developer\",\"item\":\"https:\/\/www.codemotion.com\/magazine\/frontend\/mobile-dev\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Async code on Kotlin: coroutines VS RxJava\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#website\",\"url\":\"https:\/\/www.codemotion.com\/magazine\/\",\"name\":\"Codemotion Magazine\",\"description\":\"We code the future. Together\",\"publisher\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.codemotion.com\/magazine\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#organization\",\"name\":\"Codemotion\",\"url\":\"https:\/\/www.codemotion.com\/magazine\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/11\/codemotionlogo.png\",\"contentUrl\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/11\/codemotionlogo.png\",\"width\":225,\"height\":225,\"caption\":\"Codemotion\"},\"image\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/Codemotion.Italy\/\",\"https:\/\/x.com\/CodemotionIT\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#\/schema\/person\/f9c99d8dfb99900d709bb8199225d313\",\"name\":\"Vito Gentile\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/ebcb5b885a4f1669578d08a55deb81064893c07c1b35b7b36eb059cce730ae90?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/ebcb5b885a4f1669578d08a55deb81064893c07c1b35b7b36eb059cce730ae90?s=96&d=mm&r=g\",\"caption\":\"Vito Gentile\"},\"description\":\"I\u2019m a data scientist, tech writer, software developer with experience in mobile, web (full-stack) and Python programming, and former researcher with interests in human-computer interaction. I thus have a multi-faceted experience in the area of software development, and that\u2019s why I love my job(s)!\",\"sameAs\":[\"https:\/\/vitogentile.it\",\"https:\/\/www.linkedin.com\/in\/vitogentile\/en\",\"https:\/\/x.com\/ViGentile\"],\"url\":\"https:\/\/www.codemotion.com\/magazine\/author\/vito-gentile\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Async code on Kotlin: coroutines VS RxJava - Codemotion Magazine","description":"Two main approaches to implementing asynchronous code in Kotlin: the first using coroutines, the second based on the RxJava library.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.codemotion.com\/magazine\/frontend\/mobile-dev\/async-code-on-kotlin-coroutines-vs-rxjava\/","og_locale":"en_US","og_type":"article","og_title":"Async code on Kotlin: coroutines VS RxJava","og_description":"Two main approaches to implementing asynchronous code in Kotlin: the first using coroutines, the second based on the RxJava library.","og_url":"https:\/\/www.codemotion.com\/magazine\/frontend\/mobile-dev\/async-code-on-kotlin-coroutines-vs-rxjava\/","og_site_name":"Codemotion Magazine","article_publisher":"https:\/\/www.facebook.com\/Codemotion.Italy\/","article_published_time":"2020-08-13T09:50:00+00:00","article_modified_time":"2020-09-01T14:28:15+00:00","og_image":[{"width":1012,"height":675,"url":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/02\/zan-gxW3aq6onU-unsplash-scaled.jpg","type":"image\/jpeg"}],"author":"Vito Gentile","twitter_card":"summary_large_image","twitter_title":"Async code on Kotlin: coroutines VS RxJava","twitter_creator":"@ViGentile","twitter_site":"@CodemotionIT","twitter_misc":{"Written by":"Vito Gentile","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.codemotion.com\/magazine\/frontend\/mobile-dev\/async-code-on-kotlin-coroutines-vs-rxjava\/#article","isPartOf":{"@id":"https:\/\/www.codemotion.com\/magazine\/frontend\/mobile-dev\/async-code-on-kotlin-coroutines-vs-rxjava\/"},"author":{"name":"Vito Gentile","@id":"https:\/\/www.codemotion.com\/magazine\/#\/schema\/person\/f9c99d8dfb99900d709bb8199225d313"},"headline":"Async code on Kotlin: coroutines VS RxJava","datePublished":"2020-08-13T09:50:00+00:00","dateModified":"2020-09-01T14:28:15+00:00","mainEntityOfPage":{"@id":"https:\/\/www.codemotion.com\/magazine\/frontend\/mobile-dev\/async-code-on-kotlin-coroutines-vs-rxjava\/"},"wordCount":863,"publisher":{"@id":"https:\/\/www.codemotion.com\/magazine\/#organization"},"image":{"@id":"https:\/\/www.codemotion.com\/magazine\/frontend\/mobile-dev\/async-code-on-kotlin-coroutines-vs-rxjava\/#primaryimage"},"thumbnailUrl":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/02\/zan-gxW3aq6onU-unsplash-scaled.jpg","keywords":["Codemotion Milan","Kotlin","Mobile"],"articleSection":["Mobile Developer"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.codemotion.com\/magazine\/frontend\/mobile-dev\/async-code-on-kotlin-coroutines-vs-rxjava\/","url":"https:\/\/www.codemotion.com\/magazine\/frontend\/mobile-dev\/async-code-on-kotlin-coroutines-vs-rxjava\/","name":"Async code on Kotlin: coroutines VS RxJava - Codemotion Magazine","isPartOf":{"@id":"https:\/\/www.codemotion.com\/magazine\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.codemotion.com\/magazine\/frontend\/mobile-dev\/async-code-on-kotlin-coroutines-vs-rxjava\/#primaryimage"},"image":{"@id":"https:\/\/www.codemotion.com\/magazine\/frontend\/mobile-dev\/async-code-on-kotlin-coroutines-vs-rxjava\/#primaryimage"},"thumbnailUrl":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/02\/zan-gxW3aq6onU-unsplash-scaled.jpg","datePublished":"2020-08-13T09:50:00+00:00","dateModified":"2020-09-01T14:28:15+00:00","description":"Two main approaches to implementing asynchronous code in Kotlin: the first using coroutines, the second based on the RxJava library.","breadcrumb":{"@id":"https:\/\/www.codemotion.com\/magazine\/frontend\/mobile-dev\/async-code-on-kotlin-coroutines-vs-rxjava\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.codemotion.com\/magazine\/frontend\/mobile-dev\/async-code-on-kotlin-coroutines-vs-rxjava\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.codemotion.com\/magazine\/frontend\/mobile-dev\/async-code-on-kotlin-coroutines-vs-rxjava\/#primaryimage","url":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/02\/zan-gxW3aq6onU-unsplash-scaled.jpg","contentUrl":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/02\/zan-gxW3aq6onU-unsplash-scaled.jpg","width":1012,"height":675,"caption":"mobile development developer"},{"@type":"BreadcrumbList","@id":"https:\/\/www.codemotion.com\/magazine\/frontend\/mobile-dev\/async-code-on-kotlin-coroutines-vs-rxjava\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.codemotion.com\/magazine\/"},{"@type":"ListItem","position":2,"name":"Frontend","item":"https:\/\/www.codemotion.com\/magazine\/frontend\/"},{"@type":"ListItem","position":3,"name":"Mobile Developer","item":"https:\/\/www.codemotion.com\/magazine\/frontend\/mobile-dev\/"},{"@type":"ListItem","position":4,"name":"Async code on Kotlin: coroutines VS RxJava"}]},{"@type":"WebSite","@id":"https:\/\/www.codemotion.com\/magazine\/#website","url":"https:\/\/www.codemotion.com\/magazine\/","name":"Codemotion Magazine","description":"We code the future. Together","publisher":{"@id":"https:\/\/www.codemotion.com\/magazine\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.codemotion.com\/magazine\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.codemotion.com\/magazine\/#organization","name":"Codemotion","url":"https:\/\/www.codemotion.com\/magazine\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.codemotion.com\/magazine\/#\/schema\/logo\/image\/","url":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/11\/codemotionlogo.png","contentUrl":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/11\/codemotionlogo.png","width":225,"height":225,"caption":"Codemotion"},"image":{"@id":"https:\/\/www.codemotion.com\/magazine\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/Codemotion.Italy\/","https:\/\/x.com\/CodemotionIT"]},{"@type":"Person","@id":"https:\/\/www.codemotion.com\/magazine\/#\/schema\/person\/f9c99d8dfb99900d709bb8199225d313","name":"Vito Gentile","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.codemotion.com\/magazine\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/ebcb5b885a4f1669578d08a55deb81064893c07c1b35b7b36eb059cce730ae90?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ebcb5b885a4f1669578d08a55deb81064893c07c1b35b7b36eb059cce730ae90?s=96&d=mm&r=g","caption":"Vito Gentile"},"description":"I\u2019m a data scientist, tech writer, software developer with experience in mobile, web (full-stack) and Python programming, and former researcher with interests in human-computer interaction. I thus have a multi-faceted experience in the area of software development, and that\u2019s why I love my job(s)!","sameAs":["https:\/\/vitogentile.it","https:\/\/www.linkedin.com\/in\/vitogentile\/en","https:\/\/x.com\/ViGentile"],"url":"https:\/\/www.codemotion.com\/magazine\/author\/vito-gentile\/"}]}},"featured_image_src":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/02\/zan-gxW3aq6onU-unsplash-scaled-600x400.jpg","featured_image_src_square":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/02\/zan-gxW3aq6onU-unsplash-scaled-600x600.jpg","author_info":{"display_name":"Vito Gentile","author_link":"https:\/\/www.codemotion.com\/magazine\/author\/vito-gentile\/"},"uagb_featured_image_src":{"full":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/02\/zan-gxW3aq6onU-unsplash-scaled.jpg",1012,675,false],"thumbnail":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/02\/zan-gxW3aq6onU-unsplash-scaled-150x150.jpg",150,150,true],"medium":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/02\/zan-gxW3aq6onU-unsplash-scaled-300x200.jpg",300,200,true],"medium_large":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/02\/zan-gxW3aq6onU-unsplash-scaled-768x512.jpg",768,512,true],"large":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/02\/zan-gxW3aq6onU-unsplash-scaled.jpg",1012,675,false],"1536x1536":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/02\/zan-gxW3aq6onU-unsplash-scaled.jpg",1012,675,false],"2048x2048":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/02\/zan-gxW3aq6onU-unsplash-scaled.jpg",1012,675,false],"small-home-featured":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/02\/zan-gxW3aq6onU-unsplash-scaled.jpg",100,67,false],"sidebar-featured":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/02\/zan-gxW3aq6onU-unsplash-scaled-180x128.jpg",180,128,true],"genesis-singular-images":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/02\/zan-gxW3aq6onU-unsplash-scaled-896x504.jpg",896,504,true],"archive-featured":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/02\/zan-gxW3aq6onU-unsplash-scaled-400x225.jpg",400,225,true],"gb-block-post-grid-landscape":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/02\/zan-gxW3aq6onU-unsplash-scaled-600x400.jpg",600,400,true],"gb-block-post-grid-square":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/02\/zan-gxW3aq6onU-unsplash-scaled-600x600.jpg",600,600,true]},"uagb_author_info":{"display_name":"Vito Gentile","author_link":"https:\/\/www.codemotion.com\/magazine\/author\/vito-gentile\/"},"uagb_comment_info":0,"uagb_excerpt":"A lot of Android programmers are now exploring Kotlin, a programming language based on Java and the JVM that is used to develop Android applications. As is often the case with Java-based Android apps, one of the main problems when approaching Android development in Kotlin is the management of asynchronous code. In Java, this has&#8230;&hellip;","lang":"en","_links":{"self":[{"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/posts\/255","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/comments?post=255"}],"version-history":[{"count":12,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/posts\/255\/revisions"}],"predecessor-version":[{"id":8417,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/posts\/255\/revisions\/8417"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/media\/7589"}],"wp:attachment":[{"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/media?parent=255"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/categories?post=255"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/tags?post=255"},{"taxonomy":"collections","embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/collections?post=255"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}