<?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 Nalin Porwal on Medium]]></title>
        <description><![CDATA[Stories by Nalin Porwal on Medium]]></description>
        <link>https://medium.com/@nalinporwal?source=rss-422796811e3a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*vsnXSK6luwLSA2J8.jpg</url>
            <title>Stories by Nalin Porwal on Medium</title>
            <link>https://medium.com/@nalinporwal?source=rss-422796811e3a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 27 Nov 2026 18:12:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nalinporwal/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[How to Call Multiple APIs With Dispatch Group]]></title>
            <link>https://nalinporwal.medium.com/how-to-call-multiple-apis-with-dispatch-group-bd59fe314d9a?source=rss-422796811e3a------2</link>
            <guid isPermaLink="false">https://medium.com/p/bd59fe314d9a</guid>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[threading]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[dispatchgroup]]></category>
            <dc:creator><![CDATA[Nalin Porwal]]></dc:creator>
            <pubDate>Tue, 29 Jun 2021 18:31:59 GMT</pubDate>
            <atom:updated>2021-06-29T18:31:59.324Z</atom:updated>
            <content:encoded><![CDATA[<p>Most of the time, during development we come across requirements when we want to call two or more different APIs with different parameters in a single screen to just populate data</p><p>In this post, I have explained how to use <strong>DispatchGroup </strong>to elegantly overcome this challenge and what are its other use cases. Let’s understand it with an example</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/654/1*bw1-7niu0oeHeBAxXYvbxw.png" /></figure><p>Let’s have a look at a Food Delivery Application.</p><p>Here is the link to my <strong>GitHub repo</strong>:</p><pre><a href="https://github.com/NukeNalin/FoodDeliveryDispatchGroupDemo.git"><strong>https://github.com/NukeNalin/FoodDeliveryDispatchGroupDemo.git</strong></a></pre><p>You can download it and check out the <a href="https://github.com/NukeNalin/FoodDeliveryDispatchGroupDemo/blob/main/FoodDeliveryDispatchGroupDemo/Core/Home/HomeViewModel/HomeViewModel.swift"><strong>HomeViewModel.swift</strong></a><strong> </strong>file for better understanding throughout this tutorial.</p><p>On the home screen, we need to get three different types of data from three different APIs. In this situation, we often use <strong>Nested Closures </strong>in which we call the First API. Then after we get a response from the First API, we call the Second API, and so on.</p><h3>Here is an example of it:</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%2FSZwKiGtavRJKEpE2P6fN%3F&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2FSZwKiGtavRJKEpE2P6fN&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/d2597c0f7f2ec8b8b147a460e2ed24db/href">https://medium.com/media/d2597c0f7f2ec8b8b147a460e2ed24db/href</a></iframe><blockquote>This is one way of doing it but it slowly converts into <strong>call-back hell 🔥🔥</strong>.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/999/1*7E8BzH0WfSFq3cwVyFLwWw.png" /></figure><p>Under this condition, we can use <strong>DispatchGroup </strong>to make our code more <strong>readable </strong>and<strong> optimized.</strong> You can use DispatchGroup where you want to complete a bunch of tasks. Which are not interdependent. Once all the tasks are completed, you can notify the user by some alert and refresh UI or anything you want. <strong>DispatchGroup</strong> has a function to notify after all tasks. By using this function, you can notify the main thread or global (Background) thread</p><h4>Let us get familiar with the syntax of DispatchGroup:</h4><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%2F6MMPEptKyJhGnCPJd075%3F&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F6MMPEptKyJhGnCPJd075&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/0523689c67370bab2570dc64cc336f80/href">https://medium.com/media/0523689c67370bab2570dc64cc336f80/href</a></iframe><p><strong>First</strong>, you need to create an <strong>object of DispatchGroup</strong>. The <strong>second</strong> thing is to <strong>Enter the function </strong>as shown below.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%2Fw4E2ZL0BfHHTaSPTSBzc%3F&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2Fw4E2ZL0BfHHTaSPTSBzc&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/ad66e4db4ec10eefb203426bc4b426f0/href">https://medium.com/media/ad66e4db4ec10eefb203426bc4b426f0/href</a></iframe><p>This is a mandatory step. In simple terms, when <strong>DispatchGroup</strong> executes the task, you will call the Enter function before you write a task.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%2FKsbJS3XcfANqDeAt9v4d%3F&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2FKsbJS3XcfANqDeAt9v4d&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/895b2bc602dfdf806647bfc1d66c1fa9/href">https://medium.com/media/895b2bc602dfdf806647bfc1d66c1fa9/href</a></iframe><p>Finally, when your task is complete, make sure you call the <strong>Leave function </strong>at the end of the task explicitly just like you called for the <strong>Enter function</strong>. <strong>Leave Function indicates DispatchGroup that the task is completed.</strong></p><p>We want to call three different APIs for the home page, first is TopPicks API, second is Cuisines API and the last one is Restaurant API</p><p><strong>The syntax will look likes this:</strong></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%2FiU2xkXjdSoBXcHBpfftQ%3F&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2FiU2xkXjdSoBXcHBpfftQ&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/7640e27be230808cacde9ef978235701/href">https://medium.com/media/7640e27be230808cacde9ef978235701/href</a></iframe><p>Now, let’s <strong>create the DispatchGroup object</strong>.</p><p>We are going to call the <strong>Enter function</strong> and then, call our first API. Next, call the <strong>Leave function </strong>at completion of the first API. Now, before calling the second API, make sure you call <strong>Enter function</strong> and you must call the<strong> Leave function </strong>after completion of the second API.</p><p><strong>You have to follow the same process for the third API.</strong></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%2FESi7dhgYu5cqJ2iErMEC%3F&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2FESi7dhgYu5cqJ2iErMEC&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/27a082c66370e0306635856bf922f5bd/href">https://medium.com/media/27a082c66370e0306635856bf922f5bd/href</a></iframe><p>At last, you have to <strong>call notify function</strong>. In my case, I am notifying the main thread to populate data in UI. You can perform one task in notify function closure as per requirement.</p><p>Now, let’s call the APIs with <strong>Call Back Approach </strong>and <strong>DispatchGroup </strong>and then compare the time taken to complete tasks by both the approaches.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FPiP7Zn_tLHY%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DPiP7Zn_tLHY&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FPiP7Zn_tLHY%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/71e64c460d7e359e674c07576aad6605/href">https://medium.com/media/71e64c460d7e359e674c07576aad6605/href</a></iframe><p>As you can see, the time in DispatchGroup is very less as compared to Call Back Approach. <strong>DispatchGroup</strong> takes only<strong> 2 seconds</strong> to complete the tasks, on other hand <strong>Call Back Approach </strong>tasks take <strong>6 seconds.</strong></p><p>And that’s how we can <strong>Call Multiple APIs With the Dispatch Group.</strong></p><p>Hope your concepts have been cleared. Thanks for reading my tutorial. For more such articles, you can follow. I will be sharing new weekly content, stay tuned!</p><p><strong>Keep learning and keep coding!</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bd59fe314d9a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[learn Swift Property Observers in 1 min]]></title>
            <link>https://nalinporwal.medium.com/learn-swift-property-observers-in-1-min-10caab273c8e?source=rss-422796811e3a------2</link>
            <guid isPermaLink="false">https://medium.com/p/10caab273c8e</guid>
            <category><![CDATA[swiftui]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[property]]></category>
            <category><![CDATA[combine]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Nalin Porwal]]></dc:creator>
            <pubDate>Tue, 17 Sep 2019 19:11:18 GMT</pubDate>
            <atom:updated>2019-09-17T19:11:18.454Z</atom:updated>
            <content:encoded><![CDATA[<p>Property observers are reacting or respond when there is a change in the property’s value.</p><pre>var myProperty = 5 {<br>    willSet {<br>         print(&quot;Will set to \(newValue). It was previously \     (myProperty)&quot;)<br>    }<br>didSet {<br>        print(&quot;Did set to \(myProperty). It was previously \(oldValue)&quot;)</pre><pre>} }</pre><pre>myProperty = 6<br>// prints: Will set to 6, It was previously 5<br>// prints: Did set to 6. It was previously 5</pre><p>willSet is called <strong>before </strong>myProperty is set. The new value is available as newValue, and the old value is still available as myProperty.In will set block you get newValue which is assigned value and you can do the task in willSet block and the willSet block is executed before value get change</p><p>didSet is called <strong>after </strong>myProperty is set. The old value is available as oldValue, and the new value is now available as myProperty .and didSet block is executed after the value is assigned</p><p><strong>Note: </strong>didSet and willSet will not be called in the following cases:</p><ol><li>Assigning an initial value</li></ol><p>2. Modifying the variable within its own didSet or willSet</p><p>The parameter names for oldValue and newValue of didSet and willSet can also be declared to increase readability</p><pre>var myFontSize = 10 {<br>    willSet(newFontSize) {<br>        print(&quot;Will set font to \(newFontSize), it was \ .   (myFontSize)&quot;)<br>     }</pre><pre>didSet(oldFontSize) {<br>        print(&quot;Did set font to \(myFontSize), it was \(oldFontSize)&quot;)<br>   }<br> }</pre><p><strong>Caution: </strong>While it is supported to declare setter parameter names, one should be cautious not to mix names up:</p><p>willSet(oldValue) and didSet(newValue) are entirely legal, but will considerably confuse readers of your code.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=10caab273c8e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>