<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by JItesh SUvarna on Medium]]></title>
        <description><![CDATA[Stories by JItesh SUvarna on Medium]]></description>
        <link>https://medium.com/@jitesh313?source=rss-a26c1845a7bb------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*CDvDfUMOc0U3e00HwqFwIA.png</url>
            <title>Stories by JItesh SUvarna on Medium</title>
            <link>https://medium.com/@jitesh313?source=rss-a26c1845a7bb------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 14:04:39 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jitesh313/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Network Calls: Kotlin Coroutine + Retrofit 2]]></title>
            <link>https://medium.com/@jitesh313/network-calls-kotlin-coroutine-retrofit-2-e058ebc56189?source=rss-a26c1845a7bb------2</link>
            <guid isPermaLink="false">https://medium.com/p/e058ebc56189</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[networking]]></category>
            <category><![CDATA[coroutine]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[retrofit2]]></category>
            <dc:creator><![CDATA[JItesh SUvarna]]></dc:creator>
            <pubDate>Tue, 15 Jan 2019 10:12:19 GMT</pubDate>
            <atom:updated>2019-01-15T10:12:19.403Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*4Oz-z7hm132YQWRO" /><figcaption>Photo by <a href="https://unsplash.com/@dimonblr?utm_source=medium&amp;utm_medium=referral">Dimon Blr</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Concurrency, Consistency, Parallel Processing =&gt; multiple names but suggesting the same requirement for simple task for computation of different task all at the same time and ensuring the expected output.</p><h4>A Little Background</h4><p>Lets get into the Android Realm, asynchronous code is at the core of the Android Development. Every network request should be on background thread and later the respective response must be handled on the Main/UI thread. This whole operation or monkey business needed good amount of boiler plates to setup and if you have a descent size project this can go havoc in few weeks. Enough of background story, let discussion various ways to achieve the same.</p><ul><li><strong>AsyncTask — </strong>Run short operations in sequence, also can be done concurrently. (Boilerplate code, Tedious cancellation and failure handling)</li><li><strong>Executors — </strong>Running tasks concurrently with fixed thread pool. (No direct access to UI thread, Needs micro managing)</li><li><strong>Intent service — </strong>Run long operations in sequence, handled by a queue. (No direct access to UI thread by default, Tasks are queued)</li><li><strong>RxJava — </strong>Using<strong> </strong>Observer and Observable for handling stream of data. (Steep learning curve, Mainly a event processing library)</li></ul><h4>New Kid on the Block: Coroutine</h4><p>Corountine in Kotlin helps to deal with <strong>asynchronous </strong>task in a <strong>sequential</strong>/<strong>imperative </strong>way. Using coroutine is cheaper than using thread and also the management is way easier/straightforward. Beginning with <strong>Kotlin 1.3</strong>, coroutines are available in stable version evolving from experimental stage.</p><p>We are not gonna cover coroutine in depth in this session. Many blogs explaining them neatly can be found around internet [this blog is inspired from many of them 😜]</p><h4>Setting up gradle</h4><p>Add the following dependencies to build.gradle at app level module.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/29d8eaea737c60c91ce9fcee8a9e6d97/href">https://medium.com/media/29d8eaea737c60c91ce9fcee8a9e6d97/href</a></iframe><p>Coroutine returns a future object [a object that holds the values that are populated at a later point in time] called Deferred. This are similar to other techniques for storing value of async operations like Java’s Future or Javascript Promise.</p><p>For working with Retrofit, we need special CallAdapter [that returns the values from network calls] provided by <a href="https://github.com/JakeWharton/retrofit2-kotlin-coroutines-adapter">Kotlin Coroutine Adapter</a>.</p><h4>Defining API Interface for Retrofit</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/de8aa509b228ba206c55ef2456cb69ef/href">https://medium.com/media/de8aa509b228ba206c55ef2456cb69ef/href</a></iframe><p>We are returning the Deferred object of the User defined type in this case EmployeeListModel.</p><h4>Configuring Retrofit API</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7308a48a3fe56e6716b20fd2ae13fce7/href">https://medium.com/media/7308a48a3fe56e6716b20fd2ae13fce7/href</a></iframe><p>We added addCallAdapterFactory(CoroutineCallAdapterFactory()) for getting the Deferred object, later on which we can perform async operations seamlessly.</p><h4>Async API Call On Background thread</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3971a13156b06478998d90c06164a7b2/href">https://medium.com/media/3971a13156b06478998d90c06164a7b2/href</a></iframe><p>Using the Dispatchers.IO the api is invoked on a background thread, then we await for the response without using the any callback.</p><h4>Handling UI updates On Main thread</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aa71d0cc2042ec06eaeefefd6f28f1ba/href">https://medium.com/media/aa71d0cc2042ec06eaeefefd6f28f1ba/href</a></iframe><p>Using the Dispatchers.Main will render the UI with the response on the UI/Main thread without any further handling.</p><h3>Conclusion</h3><p>Using Kotlin coroutine with Retrofit makes the code more readable. Making Asynchronous Code callback free in a very neat concise way. Now as it is available on stable version plus integration with network libraries like Retrofit, managing large Code base will be more trouble-free.</p><p>Working example is able on my <a href="https://github.com/DroidLove/AsyncDroid"><strong><em>Github</em></strong></a> repo.</p><p>For in depth overview on Kotlin Coroutines you can refer my talk on basis setup and other implementation on the same.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2Frer28zWGCkA%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Drer28zWGCkA&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2Frer28zWGCkA%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/1bd79bbd16f5e9144e5b211d3d613d3f/href">https://medium.com/media/1bd79bbd16f5e9144e5b211d3d613d3f/href</a></iframe><h3>Source</h3><ul><li><a href="https://blog.oozou.com/handle-complex-network-call-with-kotlin-coroutine-retrofit-2-30a6cd1e0189">https://blog.oozou.com/handle-complex-network-call-with-kotlin-coroutine-retrofit-2-30a6cd1e0189</a></li><li><a href="https://proandroiddev.com/kotlin-coroutines-handling-concurrency-like-a-pro-retrofit2-coroutines-31cd0611fd91">https://proandroiddev.com/kotlin-coroutines-handling-concurrency-like-a-pro-retrofit2-coroutines-31cd0611fd91</a></li><li><a href="https://kotlinlang.org/docs/reference/coroutines-overview.html">https://kotlinlang.org/docs/reference/coroutines-overview.html</a></li></ul><p>Any improvements are always welcomed. Thanks!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e058ebc56189" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>