<?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 Uddyami Agrawal on Medium]]></title>
        <description><![CDATA[Stories by Uddyami Agrawal on Medium]]></description>
        <link>https://medium.com/@uddyami?source=rss-b79a9ddcba45------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*BVkOc3XKVaZ2KsZf.jpeg</url>
            <title>Stories by Uddyami Agrawal on Medium</title>
            <link>https://medium.com/@uddyami?source=rss-b79a9ddcba45------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 04:57:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@uddyami/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[Debugging the Green: Mobile Projects, One Stroke at a Time]]></title>
            <link>https://medium.com/@uddyami/debugging-the-green-mobile-projects-one-stroke-at-a-time-dc20f1994929?source=rss-b79a9ddcba45------2</link>
            <guid isPermaLink="false">https://medium.com/p/dc20f1994929</guid>
            <category><![CDATA[mobile-engineering]]></category>
            <category><![CDATA[project-management]]></category>
            <category><![CDATA[golf]]></category>
            <dc:creator><![CDATA[Uddyami Agrawal]]></dc:creator>
            <pubDate>Mon, 04 Aug 2025 03:08:58 GMT</pubDate>
            <atom:updated>2025-08-04T03:08:58.419Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rPDhaKAfEy3JRivFkczWmQ.jpeg" /></figure><p>In the dynamic landscape of mobile engineering, the journey from concept to app store often feels remarkably like a round of golf. What began for me as a simple way to connect with my husband on the golf course soon revealed profound parallels to the complex, ever-evolving world of building and shipping mobile applications. Every swing, every green, every unexpected bounce — they all echoed the triumphs and tribulations of mobile project management.</p><h3>The Short Game: Delivering Apps on a Knife Edge</h3><p>In golf, the <strong>short game</strong> is where precision is paramount, much like the intense final stages of a mobile project leading up to <strong>delivery time</strong>. A minor miscalculation with a putter can send your ball astray, mirroring how small changes or unforeseen issues can drastically alter an app’s release.</p><ul><li><strong>CI/CD Broke — Putter Shot Line Off:</strong> Just like a putter shot that’s slightly off, leading to a missed hole, a <strong>Continuous Integration/Continuous Deployment (CI/CD) pipeline breaking</strong> at the last minute can completely derail a release. A tiny configuration error or a failed dependency update can bring everything to a halt, making that critical delivery impossible.</li><li><strong>New Requirements Found During Last-Minute Testing — Greens Had Sloping:</strong> Imagine finding out the greens have an unexpected slope just as you’re about to putt. This is akin to <strong>new requirements or critical bugs discovered during last-minute testing</strong> (e.g., a major performance issue surfacing on a low-end device, or an unexpected UI glitch on a specific screen size). The ball (your app) rolls far away from the intended target (a smooth launch).</li><li><strong>Apple Rejected the App — Ball Reached the Hole But Comes Out:</strong> The classic golf frustration — your ball looks like it’s in, then spins out. This is the mobile engineer’s nightmare: <strong>Apple or Google rejecting your app</strong> after a successful build and submission. It could be for a minor guideline violation, an overlooked permission, or an unexpected crash during their review — the “ball” reached the store, but was pulled right back out.</li></ul><h3>The Long Game: Building for Sustainable Mobile Success</h3><p>Beyond the immediate delivery, the <strong>long game</strong> in golf is about consistent performance and strategy across the entire course. In mobile engineering, this translates to <strong>learning how to run projects reliably and in a repeatable manner</strong>. This means establishing robust processes that ensure every new feature or update is built on a stable foundation.</p><p>For mobile teams, the long game involves:</p><ul><li><strong>Establishing reliable CI/CD pipelines:</strong> So builds are consistent and deployments automated.</li><li><strong>Implementing comprehensive automated testing:</strong> Across various devices and OS versions.</li><li><strong>Creating predictable release trains:</strong> Allowing for scheduled updates and hotfixes.</li><li><strong>Building a strong architectural foundation:</strong> That can scale and adapt to new technologies.</li></ul><h3>Different Clubs, Different Mobile Approaches</h3><p>Just as a golfer switches between <strong>different clubs</strong> for varying distances and terrains, mobile projects demand <strong>different approaches and team compositions</strong> at distinct stages.</p><ul><li><strong>Driver (Architects &amp; Product Owners):</strong> For the initial “drive” — defining the core vision, architecture, and technology stack (native iOS/Android vs. cross-platform frameworks like React Native or Flutter).</li><li><strong>Irons (Feature Teams):</strong> When building out specific features — cross-functional teams (developers, designers, QA) focusing on specific user stories and integrations.</li><li><strong>Wedge/Putter (QA &amp; Release Managers):</strong> For the intricate “short game” around release — dedicated QA testing across device fragmentation, performance tuning, and navigating app store submission complexities.</li></ul><p>Learning to “rotate your clubs” means knowing when to leverage platform specialists, when to invest in a specific testing methodology (e.g., performance testing, security audits), or when to prioritize UX refinement over raw feature development.</p><h3>When the Ball Goes to the Rough: Mobile Incident Response</h3><p>In golf, the <strong>ball often goes to the rough</strong> — a perfect metaphor for when a <strong>plan goes sideways</strong>. In mobile engineering, the “rough” can be particularly thorny:</p><ul><li><strong>Critical Crash Spikes:</strong> A seemingly small code change leads to widespread crashes on a specific device type.</li><li><strong>Major OS Update Breakages:</strong> A new iOS or Android version drops, and suddenly a core app feature stops working as expected.</li><li><strong>Unexpected SDK Conflicts:</strong> Integrating a new third-party SDK breaks existing functionality.</li></ul><p>The key isn’t to avoid the rough entirely (impossible in mobile), but to <strong>learn how to recover and plan for failure</strong>. This means having robust monitoring, real-time crash reporting, swift hotfix deployment capabilities, and clear communication plans for your users. Planning for failure means having rollback strategies and redundant systems in place.</p><h3>External Factors: The Unpredictable Mobile Environment</h3><p>In golf, <strong>external factors</strong> like wind, terrain, or a tricky bounce can heavily influence your shot’s outcome, regardless of your swing. This resonates deeply with mobile projects. Once you release the app, its success isn’t solely dependent on your team’s execution.</p><ul><li><strong>App Store Review Policies:</strong> Sudden changes in review guidelines or unexpected delays can impact launch timelines.</li><li><strong>Competitor Launches:</strong> A new, innovative competitor app can shift market dynamics overnight.</li><li><strong>New Devices &amp; OS Versions:</strong> The release of a new iPhone or Android OS can introduce unforeseen compatibility issues or new user expectations.</li><li><strong>Network Conditions:</strong> Your app’s performance can be impacted by varying network speeds and reliability in the real world.</li></ul><h3>What I’ve Learned and Enjoyed as the Game (and Mobile Dev) Grew on Me</h3><p>The deeper I delved into both golf and mobile engineering, the more these parallel lessons resonated, shaping my approach to leadership and problem-solving.</p><ul><li><strong>Commitment to the Shot (or Build):</strong> You have to <strong>make your mind to take the shot</strong>. In mobile, indecision on architectural choices or a delayed release commitment can lead to worse outcomes than a confident, even if imperfect, decision.</li><li><strong>Embrace All Clubs (and Tools):</strong> <strong>All clubs are there to help your game.</strong> Leverage every tool available: robust analytics platforms, crash reporting tools, device farms for testing, A/B testing frameworks, and advanced CI/CD solutions.</li><li><strong>The Constant Player in a Changing Environment:</strong> The <strong>environment and game will always change</strong>(new OS, new devices, new user trends), but the <strong>only constant is the player</strong> — your team’s ability to adapt, learn, and remain resilient.</li><li><strong>Active Reflection, Adaptable Application:</strong> While <strong>active reflection</strong> on past releases or incidents is vital (post-mortems, retrospectives), you can’t blindly apply last sprint’s learnings to the very next. Every new feature, every new OS version, requires a tailored setup and approach.</li><li><strong>Routines for Readiness:</strong> <strong>Routines help your brain get ready for the shot.</strong> For mobile teams, this means having consistent pre-release checklists, daily stand-ups, definition of done, and rigorous code review processes to ensure readiness for each build.</li><li><strong>Practice Like It’s a Match, Grace for Failures:</strong> <strong>Practice like it’s a match</strong> by running frequent internal alpha/beta tests. Have the <strong>grace to allow failures to happen</strong> — a test suite that fails, or even a production bug, is a learning opportunity that will make tomorrow’s app better.</li><li><strong>Consistency and Discipline:</strong> The <strong>consistency and discipline to stick through the adjustment phase</strong>(like re-architecting a module or fixing flaky tests) are worth it, leading to a more stable and reliable app.</li><li><strong>Strategic Feedback:</strong> I learned that <strong>asking for feedback can really improve your game</strong>, but for mobile, knowing <em>when</em> to ask (e.g., A/B test results, user reviews, crash logs) and <em>how</em> to implement it is crucial. Blindly following every user comment can lead to confusion; synthesizing feedback to realign your strategy is key for the next iteration.</li><li><strong>Adapting to Variability:</strong> <strong>No game is the same; adjustments depend on the weather, player fatigue.</strong>In mobile, this translates to adapting to device fragmentation, varying network conditions, or even team fatigue affecting code quality.</li><li><strong>Celebrating Wins, Big and Small:</strong> Whether it’s shipping a challenging feature on time, squashing a critical bug, seeing positive app store reviews, or achieving a new user engagement metric — <strong>celebrating small wins and big wins</strong> keeps the team motivated and reinforces the value of their hard work.</li></ul><p>My initial drive to learn golf was simply to spend more time talking with my husband. Today, that simple motivation continues, but it’s intertwined with the thrill of seeing my game improve drastically in last two years. Every team member in mobile engineering, too, can have their own unique motivations — be it mastering a new framework, seeing millions of users engage with their work, or simply solving complex technical puzzles. Understanding and aligning these diverse motivations is just as crucial as any technical skill in delivering a truly impactful mobile experience.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dc20f1994929" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Networking in Android & Kotlin: Anko]]></title>
            <link>https://medium.com/kotlin-thursdays/networking-in-android-kotlin-anko-c5bd21678452?source=rss-b79a9ddcba45------2</link>
            <guid isPermaLink="false">https://medium.com/p/c5bd21678452</guid>
            <category><![CDATA[networking]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[kotlin-thursdays]]></category>
            <dc:creator><![CDATA[Uddyami Agrawal]]></dc:creator>
            <pubDate>Thu, 25 Apr 2019 13:01:00 GMT</pubDate>
            <atom:updated>2019-04-25T14:44:36.494Z</atom:updated>
            <content:encoded><![CDATA[<h3>Networking in Android &amp; Kotlin</h3><h4>Let’s play with Anko</h4><p>For those that might not know, this application had been inspired and started long ago from a true love story on Tinder of a dedicated Avocado giving Avocado facts for a year straight.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/720/0*jPwN7bQ0NbYDu66H" /></figure><p>In the <a href="https://medium.com/kotlin-thursdays/networking-in-android-kotlin-94db3fde1874">previous post</a>, we had improved upon the existing application by updating the UI by fetching a random Avocado fact using AsyncTask. Before we run to start working with databases I wanted to check if the code is really KOTLIN material. And I was asking myself but with Kotlin around, why should I still use pure Java APIs? We explore that today with <strong>Anko</strong>. <strong>Anko</strong> is described on GitHub as “pleasant Android application development”. It’s all about making Android authentically Kotlin. It can almost make you love Android OS APIs (or maybe just not be mad at them). In this post, we will set up Anko doAsync like a pro!</p><h4>References</h4><ol><li><strong>Antonio Leiva’s <em>Using Anko to run background tasks with Kotlin in Android</em>: </strong><a href="https://antonioleiva.com/anko-background-kotlin-android/">https://antonioleiva.com/anko-background-kotlin-android/</a></li><li><strong>Anko Async.kt: </strong><a href="https://github.com/Kotlin/anko/blob/38cada163c3ef0bfadc929cd3d68b436d5f10d51/anko/library/static/commons/src/main/java/Async.kt">https://github.com/Kotlin/anko/blob/38cada163c3ef0bfadc929cd3d68b436d5f10d51/anko/library/static/commons/src/main/java/Async.kt</a></li><li><strong>Future:</strong> <a href="https://developer.android.com/reference/java/util/concurrent/Future">https://developer.android.com/reference/java/util/concurrent/Future</a></li></ol><h4>Anko, explained</h4><p>If vanilla Kotlin had you excited, then “Anko” will take it level 99. Code becomes very readable and concise. Not deviating too much, let’s look at AsyncTask Anko style.</p><p>There is a lot of decision-making that is needed before even starting any actual code. Check out “Anko” structure with all the syntax removed. The following is empty Async Task syntax. No steps involved, just two blocks with clear thread understanding.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/41e1e5afa0ec4636cb7d7ddd679146e2/href">https://medium.com/media/41e1e5afa0ec4636cb7d7ddd679146e2/href</a></iframe><p>doAsync is all that is required to ask the OS to run the following block in a separate thread, while uiThread brings our result back to our Main thread.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d36a8b158e655bbd4ed126f67933ace6/href">https://medium.com/media/d36a8b158e655bbd4ed126f67933ace6/href</a></iframe><p>It only took me couple of minutes to make all changes from AsyncTask to doAsync {...}. The code is super clear and with WeakReference support added, it ensures call the to uiThread block runs for Activity or Fragment that is actually alive and not keep track of dead UI. I would love to hear how long it took for you guys to convert your AsyncTask!</p><p>You can see the complete Github repo here:</p><p><a href="https://github.com/Kotlin-Thursdays/Avocado-Facts">Kotlin-Thursdays/Avocado-Facts</a></p><p>Anko helps use true KOTLIN constructs from ground up and is supported by JetBrains and Google. This is the new support library we cannot live without. I would recommend to check this huge library out. There are numerous nuggets out there to found.</p><p>We’ve set up our Async Task in Anko we will try and hit an actual api to retrieve our fact , See you next week!</p><h4>But for all my Curious Georges’ out there .. what’s underneath the hood</h4><p>It looks so nice! How did Anko achieve it?</p><p>From the <a href="https://github.com/Kotlin/anko/blob/38cada163c3ef0bfadc929cd3d68b436d5f10d51/anko/library/static/commons/src/main/java/Async.kt">Async.kt</a> class itself:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f20d779515c6a6687584b857f486aa8/href">https://medium.com/media/1f20d779515c6a6687584b857f486aa8/href</a></iframe><p>Mind. Blown.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*IWplt5Ryk7xuYrLU" /></figure><p>Let’s break this code down a little bit.</p><p>Anko devs rewrote the async API from scratch — loopers and everything! Let’s do quick recap for the asynchronous task operation API. The most low level OS API we have is called Future (<a href="https://developer.android.com/reference/java/util/concurrent/Future">doc</a>). Future is like <strong>result holder </strong>of an asynchronous computation which will come back at some point later in time. Generally it is running on a different thread and some limited functionality to communicate with it. This lead to two new concepts called callable (<a href="https://developer.android.com/reference/java/util/concurrent/Callable?hl=en">doc</a>) and promise (<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.js/-promise/index.html">doc</a>). We can use a metaphor to understand the two — if a company is callable they give you a number to call, generally no arguments are passed in but just call(). While promise is more human words like.. I promise you I will do this but I may error. promise at least comes with one assurance that it will have some response on your request. Next in line for ASYNC concepts is Runnable, it will only run the block and doesn’t have capability to return anything. We can configure which thread it needs to run and it will run on it.</p><p>These api’s are all from OS to help do ASYNC tasks.</p><p>doAsync returns a Future&lt;Unit&gt;. Futures are […]. According to <a href="https://kotlinlang.org/docs/tutorials/coroutines/async-programming.html">Kotlin documentation</a> -</p><blockquote>The idea behind futures or promises (there are also other terms these can be referred to depending on language/platform), is that when we make a call, we’re promised that at some point it will return with an object called a Promise, which can then be operated on.</blockquote><p>One place we all might be using these concepts already is Retrofit. Since it be OnPostExecute or uiThread they both are callback blocks that are called after the background work has returned it result. It is this reason that we keep track who called me or who was I promised ? To cancel or stop listening for response we make this reference to be nulled anytime aka always only keep WeakReference. Android OS has made this data object <strong><em>exclusively</em></strong> to allow garbage collector to collect an object even when someone is trying to keep track of it.</p><p><strong>This explanation was over simplified. I would highly recommend reading actual docs for technical details. See you next time!</strong></p><h3>Navigate Networking in Android here:</h3><ul><li><strong>Part 1 — Back to Basics with Async Tasks: </strong><a href="https://medium.com/kotlin-thursdays/networking-in-android-kotlin-94db3fde1874">https://medium.com/kotlin-thursdays/networking-in-android-kotlin-94db3fde1874</a></li><li><strong>Part 2 — Let’s Play with Anko:</strong> <a href="https://medium.com/kotlin-thursdays/networking-in-android-kotlin-anko-c5bd21678452">https://medium.com/kotlin-thursdays/networking-in-android-kotlin-anko-c5bd21678452</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c5bd21678452" width="1" height="1" alt=""><hr><p><a href="https://medium.com/kotlin-thursdays/networking-in-android-kotlin-anko-c5bd21678452">Networking in Android &amp; Kotlin: Anko</a> was originally published in <a href="https://medium.com/kotlin-thursdays">Kotlin Thursdays</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Networking in Android & Kotlin]]></title>
            <link>https://medium.com/kotlin-thursdays/networking-in-android-kotlin-94db3fde1874?source=rss-b79a9ddcba45------2</link>
            <guid isPermaLink="false">https://medium.com/p/94db3fde1874</guid>
            <category><![CDATA[networking]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[kotlin-thursdays]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[asynctask]]></category>
            <dc:creator><![CDATA[Uddyami Agrawal]]></dc:creator>
            <pubDate>Thu, 11 Apr 2019 13:01:01 GMT</pubDate>
            <atom:updated>2019-04-25T14:44:54.358Z</atom:updated>
            <content:encoded><![CDATA[<h4>Back to Basics with Async Tasks</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kY5JiAc8ZgO6YI7Vpv__kA.png" /></figure><p>We have an existing Android Avocado Facts application created in an <a href="https://medium.com/kotlin-thursdays/kotlin-thursdays-introduction-to-android-and-kotlin-cb07606443c">older Kotlin Thursdays post</a>, but for any Android application to be seriously useful, we will have to upgrade our facts to be more than just hard-coded. Feel free to follow along with our <a href="https://github.com/Kotlin-Thursdays/Avocado-Facts">Github repo</a>. In this post, we will set up an Async Task like a pro!</p><h4><strong>References</strong></h4><ul><li><strong>Async Tasks- </strong><a href="https://developer.android.com/reference/android/os/AsyncTask">https://developer.android.com/reference/android/os/AsyncTask</a></li><li><strong>Processes and Threads in Android- </strong><a href="https://developer.android.com/guide/components/processes-and-threads">https://developer.android.com/guide/components/processes-and-threads</a></li><li><strong>WeakReference- </strong><a href="https://developer.android.com/reference/kotlin/java/lang/ref/WeakReference">https://developer.android.com/reference/kotlin/java/lang/ref/WeakReference</a></li><li><strong>Threading-</strong> <a href="https://developer.android.com/topic/performance/threads">https://developer.android.com/topic/performance/threads</a></li></ul><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FrYJ0d2ctmBk%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DrYJ0d2ctmBk&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FrYJ0d2ctmBk%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/83b8da5e8357a266024a8b9be42cc4bb/href">https://medium.com/media/83b8da5e8357a266024a8b9be42cc4bb/href</a></iframe><h3>Async Tasks in Android</h3><p><strong>Async Tasks</strong> are a lightweight threading construct given by Android OS. Being an Android-specific concept, it is not created by Java or Kotlin. Async Tasks makes most common task of doing some heavy-weight operation off main thread easy and simple.</p><p>The Android OS is able to refresh the UI 64 frames per second — to achieve this, the UI Thread, or the Main Thread, needs to be free as much as possible. Long running tasks like communicating with APIs or databases could lock up your UI thread if it is not done in a background thread. One way to achieve this is by off-loading any longer running tasks to other threads. AsyncTasks is very powerful construct provided by OS to help achieve this.[<a href="https://developer.android.com/topic/performance/threads">more details</a>]</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*1k1OKwV7SDw5TzFp" /></figure><h3><strong>Creating an AsyncTask in Android</strong></h3><p>An AsyncTask has three generic parameter types:</p><pre>AsyncTask&lt;Params, Progress, Result&gt; <br>- params: the type of the parameters sent to the task upon execution<br>- progress: the type of the progress units published during the background computation<br>- result: the type of the result of the background computation</pre><p>We can actually think about implementing our Async tasks in the order these parameter types are defined!</p><p><strong>Step 1:</strong></p><p>We must analyze if any runtime parameters need to be sent for execution of the task. In our example to generate a new fact, we only need to invoke the function. Hence, the param type is Void.</p><p><strong>Step 2:</strong></p><p>Do we need to know about the progress percentage of this task?</p><p>In our async task, there is no concept of progress hence the progress type is also Void. One example where this is crucial will be downloading a big file.</p><p><strong>Step 3:</strong></p><p>What is the result of task? The type we designate here also tells us how will we can update our UI with the result. In particular, we want to show the user new fact which is of type String.</p><p>All these steps gives us our signature</p><pre>AsyncTask&lt;Void, Void, String&gt;() {</pre><p><strong>Step 4:</strong></p><p>Start by extending the abstract class and implementing minimum</p><pre><strong>override fun </strong>doInBackground(<strong>vararg </strong>params: Void?): String {</pre><p><strong>Step 5:</strong></p><p>Any ui update if need will need.</p><pre>Shortcut tip: ctrl + o (override method shortcut in android studio) will help do method setup</pre><pre><strong>override fun </strong>onPostExecute(result: String?) {</pre><p>Here we need to set result string as Textview’s text value. We have method in activity that will update it for us. So to call it we need to pass that to task so it can call it. Changing class to have</p><pre><strong>private class </strong>NewAvocadoFactTask(<strong>val activity</strong>: MainActivity)</pre><p>Okay! Now just call is left</p><p><strong>Step 6:</strong></p><p>In onClick method call</p><pre>moarButton.setOnClickListener <strong>{<br>  </strong>NewAvocadoFactTask(<strong>this</strong>).execute();<br><strong>}</strong></pre><p>Now for the <strong>MOST</strong> important step: you are leaking your activity!</p><p>How?? What??</p><p>AsyncTask is a <em>threading</em> construct. AsyncTask started a new thread and it doesn’t know your app may or may not have been killed by the time we have our response. In other words, AsyncTask runs independent of the main app in the background thread. So if the app is closed first without first ending the task, that task could still be running. After the response is returned, AsyncTask is responsible for returning the result to the main thread itself as a callback.</p><p>Is passing the actual activity val activity: MainActivity really safe? Should we try to call something that could be garbage collected?? Maybe we should use a wrapper called <a href="https://developer.android.com/reference/kotlin/java/lang/ref/WeakReference">WeakReference</a>.</p><pre><strong>var reference</strong>: WeakReference&lt;MainActivity&gt; = WeakReference(activity)</pre><p>What is WeakReference? Glad you asked. It is a wrapper type that would be ok if the object it was keeping track of was garbage collected. Incase that happens it will indicate you the object you wanted reference for is no longer present.</p><p>reference.get() will yield a null. So we make our UI update call with elvis operator</p><pre><strong>reference</strong>.get()?.updateAvocadoFact(<strong>it</strong>)</pre><h3><strong>Gotcha(s)</strong></h3><ol><li>Always pass any context in AsyncTask with <em>WeakReference</em> Wrapper</li></ol><pre><strong>var reference</strong>: WeakReference&lt;MainActivity&gt; = WeakReference(activity)</pre><p>2. Use OnProgressUpdate() and OnPostExecute() to show something on UI</p><pre><strong>override fun </strong>onPostExecute(result: String?) {</pre><pre>Log.v(<strong>tag</strong>,<strong>”onPostExecute is running on ${</strong>Thread.currentThread().<em>name</em><strong>}”</strong>)</pre><pre><strong>  super</strong>.onPostExecute(result)<br>  result?.<em>let </em><strong>{<br>  </strong><em>// if reference.get is not null execute the function<br>  </em><strong>reference</strong>.get()?.updateAvocadoFact(<strong>it</strong>)<br>  <strong>}<br></strong>}</pre><p>Logging will help you see which functions are executed on Main thread since UI elements can only be touched by Main thread.</p><p>3. Always create a new Task object for a execute.</p><pre>moarButton.setOnClickListener <strong>{<br>    </strong>NewAvocadoFactTask(<strong>this</strong>).execute()<br><strong>}</strong></pre><p>From threading rules of <a href="https://developer.android.com/reference/android/os/AsyncTask#threading-rules">Async Task</a></p><blockquote>The task can be executed only once (an exception will be thrown if a second execution is attempted.)</blockquote><p>You can see the complete Github repo here:</p><p><a href="https://github.com/Kotlin-Thursdays/Avocado-Facts/tree/ud/async_master">Kotlin-Thursdays/Avocado-Facts</a></p><p>We’ve set up our Async Task, but we actually need the request for the task to connect to a database so we can grab our Avocado Facts. In the next post, we will set up a database in Firebase and get our Async Task connected! See you next week.</p><h3>Navigate Networking in Android here:</h3><ul><li><strong>Part 1 — Back to Basics with Async Tasks: </strong><a href="https://medium.com/kotlin-thursdays/networking-in-android-kotlin-94db3fde1874">https://medium.com/kotlin-thursdays/networking-in-android-kotlin-94db3fde1874</a></li><li><strong>Part 2 — Let’s Play with Anko:</strong> <a href="https://medium.com/kotlin-thursdays/networking-in-android-kotlin-anko-c5bd21678452">https://medium.com/kotlin-thursdays/networking-in-android-kotlin-anko-c5bd21678452</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=94db3fde1874" width="1" height="1" alt=""><hr><p><a href="https://medium.com/kotlin-thursdays/networking-in-android-kotlin-94db3fde1874">Networking in Android &amp; Kotlin</a> was originally published in <a href="https://medium.com/kotlin-thursdays">Kotlin Thursdays</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>