<?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 Jageloo Yadav on Medium]]></title>
        <description><![CDATA[Stories by Jageloo Yadav on Medium]]></description>
        <link>https://medium.com/@jageloo.yadav?source=rss-176e0bb80b84------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*MAPKqRKEJiPs0IvccYOuhg.jpeg</url>
            <title>Stories by Jageloo Yadav on Medium</title>
            <link>https://medium.com/@jageloo.yadav?source=rss-176e0bb80b84------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 08:06:09 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jageloo.yadav/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[@Binding or Binding<Value>(SwiftUI)]]></title>
            <link>https://medium.com/@jageloo.yadav/binding-or-binding-value-swiftui-22b363380875?source=rss-176e0bb80b84------2</link>
            <guid isPermaLink="false">https://medium.com/p/22b363380875</guid>
            <dc:creator><![CDATA[Jageloo Yadav]]></dc:creator>
            <pubDate>Sun, 16 Oct 2022 08:17:01 GMT</pubDate>
            <atom:updated>2022-10-16T08:17:01.700Z</atom:updated>
            <content:encoded><![CDATA[<p>Binding is a property wrapper defined in SwiftUI framework which is to create a two-way connection between a property that stores.</p><p>it is defined in framework as below:</p><p><strong>@propertyWrapper</strong> <strong>@dynamicMemberLookup</strong> <strong>public</strong> <strong>struct</strong> Binding&lt;Value&gt; {}</p><ul><li>@Binding always defined as stored property of View when this property needs to control/bind by any other external data store/property.</li><li>It can be defined either by “<strong>@Binding var variable: VarType</strong>” or “<strong>var variable: Binding&lt;ValueType&gt;</strong>”</li><li>You can not use <strong>“let”</strong> keyword to define it because it is modified by/controlled by other stored property while “let” will not allow to change.</li><li><strong><em>When you have requirement to change value in view’s state/component’s status changes your external stored property and also changes in external stored property automatically changes in view’s status then you need to create connection 2 way, for this @Binding or Binding&lt;Value&gt; should be used</em>.</strong></li><li>To bind with externellly “$” should be used. To use value of binding locally you don’t need to use $ to access value.</li><li>If defined using struct Binding&lt;Value&gt;, <strong>structObj.wrappedValue</strong> should use to populate value in view component.</li></ul><h4><strong>Examples</strong>:</h4><p><strong>1- child View’s status to parent View’s status 2 way connection/binding</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hK552ZWKVVWxI0ecwF5SVg.png" /></figure><ul><li>In above example changing value of <strong>isChecked in </strong>CheckBox<strong> on touch event </strong>changes status/value in parent View’s checkBox and vice versa</li><li>Parent view property is also defined as @Binding it means it can also be bind with other externel storage.</li></ul><p>2- <strong>Bind View to external observable Object</strong></p><p>Suppose i am binding above checkboxItem with below Model in any other supper view called ContentView coded as below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_xcuUMTNVvatzm6gew5HGg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-kPRN0GaGYI3eW90aaoe6w.png" /></figure><ul><li>if you see CheckBoxItemModel code isChecked not modified any where in its body but still its “didSSet” called and value is update. It is updated because changing isChecked inside CheckboxItemView. It is happening because there is data binding between CheckboxItemView and CheckboxItemModel.</li><li>Also if you do manual change in <strong>CheckboxItemModel’s</strong> <strong>isChecked </strong>some where in<strong> ContentView, </strong>checkbox will also change automatically<strong>. </strong>It means it has 2 way binding between<strong> CheckboxItemModel and CheckboxItemView</strong></li></ul><h3><strong>Define component publicly</strong></h3><p>To define component into framework you will have to use Binding&lt;Value&gt; instead @Binding var vv: Value because it will require public constructor where propertyWrapper Assignment needed in constructor.</p><p>example</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n2XP4f6nzVLWuqtrtCi6pw.png" /></figure><p><strong>Note</strong> — Only reading will not clear your concept until you try practicly. It will just give overview and encourage you to work and save your time.</p><h3>Happy Reading!</h3><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=22b363380875" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[iOS: MVVM design for Screen having multiple web services/multiple sources of data]]></title>
            <link>https://medium.com/@jageloo.yadav/ios-mvvm-design-for-screen-having-multiple-web-services-multiple-sources-of-data-54bffa92dec3?source=rss-176e0bb80b84------2</link>
            <guid isPermaLink="false">https://medium.com/p/54bffa92dec3</guid>
            <category><![CDATA[mvvm-ios-interview]]></category>
            <category><![CDATA[mvvm-ios]]></category>
            <category><![CDATA[mvvm-multiplewebservice]]></category>
            <dc:creator><![CDATA[Jageloo Yadav]]></dc:creator>
            <pubDate>Wed, 13 Apr 2022 11:53:38 GMT</pubDate>
            <atom:updated>2022-04-13T13:24:38.324Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>iOS: MVVM design for Screen having multiple web services/multiple sources of data</strong></p><p>Choosing a design for a screen which has multiple sources of data, multiple child view components, multiple web services is always a challenging thing. Recently I had given more than 20 interviews where I had been asked to make designs for such scenarios and also got lots of pre-interview assignments to make complex screens using MVVM.</p><p>Here are some key points that interviewers/assignment givers are actually interested in.</p><ul><li>They are actually interested in practical implementation and want to know how communication will happen among layers.</li><li>They want to know the number of classes/interfaces you will take to design it.</li><li>Whether your code is scalable/unit testable</li><li>Whether your code follows Engineering basic principal OOPs, SOLID etc.</li><li>Whether your code is clean, concise and modularise.</li></ul><p><strong>To start with screen design lets understand first what MVVM is.</strong></p><p><strong>MVVM</strong> — MVVM(Model-View-ViewModel) is a software design pattern which separates the code of User interface from code of business logic/testable logic or any custom logic to manipulate data. Basically the View model is converted model/business class from the data model.</p><p>If we talk about simple and small view design then communication would be like below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7IW69HYSCtZyZ39BorO45Q.png" /><figcaption>MVVM — Design Diagram</figcaption></figure><p>From the above image it shows that MVVM has bidirectional communication between View-Model &amp; Model and Model &amp; View Model. And Also View -&gt; View Model &amp; View Model -&gt; View. If we see the connection between the View to View Model symbol is something different which is called <strong>Binding. </strong>That means changing the state of the View model, the view’s state should be changed and vice versa.</p><p><strong>Confusion on Binding in iOS App: </strong>Sometimes developers get confused about what is binding, should we use any binding technique?. You don’t worry about binding techniques, it is just a way to make communication between view and view model. It means just sending an event to View if something changed in VIew model or if some event happened in View, notify to view model. This communication can be achieved in many ways.</p><p>To achieve binding, developers can create their own technique to listen to activity from View to ViewModel or View model to VIew if they are not aware about reactive programming like RXSwift/Combine where state changes automatically if binded with each other .Binding can be done by delegate(by passing weak reference of view without knowing View) or block, property observer etc.</p><ul><li>If you understand the binding concept correctly then we can update above image as below.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vAGi75j6w5yd7bX1bu2hZA.png" /><figcaption>MVVM on basis of communication.</figcaption></figure><p>If we convert above image into classes and entity, we can have below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0QQ-84MsFu3fw5Vfjj0JVA.png" /></figure><p>If we check the above code, the view is tightly coupled with the view model which violates dependency inversion principle. We can change above code as below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jcBP7oTdevvPfUHuHqrh8g.png" /></figure><p>Where ViewDataInterface is an abstraction for view models. If we make our view controller this way, Any view model can be passed to our view which implements ViewDataInferface abstraction.</p><p><em>Some time making protocol/interface overkills the code so you can directly use view model reference in View Controller it’s up to you and your requirement.</em></p><p><strong>View Model Class</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hiHsk4Qq5etWLhqikD0gsA.png" /></figure><p>Better if we convert the above classes and connect using class diagrams. It will look like below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sh-pVFAozsHu9c9PAvZasw.png" /></figure><p>Hope above explanation is already told about MVVM.</p><p><strong>Let’s come to the problem for which this tutorial was written.</strong></p><p><strong>Problem statement: </strong>Need to make an app that will search for a Cocktail based on the input given by the user. Search results would be a grid as shown in the <strong>below given screenshot.</strong> A few search keys are “Vodka, “Rum”, and “Mojito”. You need to consider the below points.</p><p>A few of the UI elements in the attached screenshot like “Let’s eat quality food”, “Near restaurant” and a row below it are static and should be hardcoded. The app should follow the MVVM design pattern and the language used should be Swift.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Fsll2gliaq7RzIcmE-8i-Q.png" /></figure><ul><li>If we break the screen into sections, we have 3 sections. First section contains header with textField, second section contains restaurant section which data comes from Restaurant Service and Third section contains drink section which data comes from DrinkApiService.</li></ul><p><strong>Solution — </strong>From the screen it looks like it is a collection view in 3 sections<strong>. </strong>If we follow MVVM we need to consider the whole screen as one Main View. Let’s take it as a HomeViewController as the main View.</p><p>So our first 2 classes would be HomeViewController and HomeViewModel. Lets build skeleton for this 2 classes</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DTQ8h4LYFjpNlcrSY0_Ffg.png" /></figure><ul><li>So the collection view would be the main content view which data would be supplied by HomeViewModel. But Collection view is also made with small-small child view which would need its own view model to render it.</li><li>To populate collection view lets create view model interface.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Yq2ZOAzlv1orZm7nIGDedA.png" /></figure><p>Where some of protocol used listed below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-yxiIMQ93I_opNPhIrA2VA.png" /></figure><pre>Above protocols are used to make view controllers as independent as we can. Now lets see how HomeViewController will look like</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-tlnSAZVN0siVCHiWYQntQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I19FDCjBTPzA3icYrTXA-w.png" /></figure><ul><li>If we see the above view controller code and find that there is no business class instance except ViewModel. This means all business logic required in the View controller would be fulfilled by view model.</li><li><strong>Now the question is where do we write code for networking, database, caching, tracking/analytics</strong>. These things are always debatable. Some developers keep an instance of the caching/network handler/database handler instance in the view controller and start entry of these from here. Some keep instances of these classes in View model and View will ask view model to perform tracking, caching and View model will redirect code in their responsible class.</li><li><strong>My Preference — </strong>My preference would be to keep instances of network client/analytics client in View Model instead in View controller so that Communication between View and View model always be bidirectional and no other layer comes between View and View Model<strong>.</strong></li><li>Suppose if any web service class for fetching data to display in View/View controller, View controller will notify to View model to fetch data and provide displayable data and View model will ask to network layer classes to get data and returns server data to View model so that View model gets server data and process into displayable format and supply back to View/View controller.</li><li>In our problem UI there are 2 web service calls which are triggered from the View controller via ViewModel.fetchRestaurent() and ViewModel.fetchData(querry: searchbar.text). Lets how our view model will look like.</li></ul><p><a href="https://gist.github.com/jagelooyadav/04bb16a324e109b830ae9ceeb15a5058#file-homeviewmodel-swift">https://gist.github.com/jagelooyadav/04bb16a324e109b830ae9ceeb15a5058#file-homeviewmodel-swift</a></p><p><strong>Now the question is what forms of data ViewModel will supply to ViewController because the view controller has a collection view which has many subviews.</strong></p><p>Since data is coming from different- different sources, we have to create some common/generic approach to make similarity in those data to supply uniform data to Collection View instead of making child view model instances in main view model and storing in it.</p><p>Let’s take one generic model which can handle all api called in the main view model. Since the collection view is divided into sections, we will make a section object for each section.</p><pre><strong>struct</strong> Group&lt;T&gt;: Section {</pre><pre><strong>    let</strong> groupType: GroupType</pre><pre><strong>    var</strong> title: String?</pre><pre><strong>    let</strong> groupData: [T]</pre><pre>}</pre><p>Where T is generic data returning from web services. Section is protocol which will have groupType/dataType and section title property which needs to implement each section/group object</p><p>Suppose if service1 return Restaurant object list group would Group&lt;Restaurant&gt; and if service2 returns Drink data, it will be Group&lt;Drink&gt;</p><p>Also first section is not required any data all are hard code so we can make group like Group&lt;Void&gt;</p><p>So here are section objects created for each section.</p><p><strong>Section for main header including search field</strong></p><pre><strong>let screenHeader = Group&lt;Void&gt;(groupType: .screenHeader, groupData: [])</strong></pre><p><strong>Section for restaurant</strong></p><pre><strong>let dataSupliedFromRestarentApi: Restaurent = someData</strong></pre><pre><strong>let defaultEntry = Group&lt;Restaurant&gt;(groupType: .defaultEntry,</strong></pre><pre><strong>title: “New Restaurant”,</strong></pre><pre><strong>groupData: [dataSupliedFromRestarentApi])</strong></pre><p><strong>Section for Drinks</strong></p><pre><strong>Let dataFromDirnkApi: [Drink] = someData</strong></pre><pre><strong>let section = Group&lt;Drink&gt;(groupType: .otherEntries, title: “\(data.drinks.count) Items found”, groupData: dataFromDirnkApi)</strong></pre><p>If you see the above section object I am not sending the child view model to the Main View model to populate its child but I am sending raw data to the Main view model. We can child view model objects here in the main view model but it will have some calculation unnecessarily so better to send raw model to main view controller where child view model can be created with this raw data.</p><p>So in <strong>cellForItemAt</strong> function of view controller we can create child view model for cell has below:</p><pre><strong>let</strong> section = viewModel.sections[indexPath.section]</pre><pre>If section.groupType == .entriesData, let group = section as? HomeViewModel.Group&lt;Drink&gt; {</pre><pre>let drinkData = group.groupData[index.row]</pre><pre>cell.viewModel = GridCardViewModel(drink: data)</pre><pre>}</pre><p>As I said this calculation of GridCardViewModel creation could be done in Main view mode itself but it will put unnecessarily burden in main view model calculation.</p><ul><li>Idea is to avoid storing all child view models into main view model instead supply raw data to main view there child view model can be created during child view initialization or updation. It will allow child view updates independently instead of reloading the whole View model for just reloading part of the UI.</li><li>On the basis of the above explanation I have made a generic diagram to deal with multiple web services in MVVM design pattern please see below diagram.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0ersFJLEmApBswbTYZCgog.png" /><figcaption><strong>Generic MVVM diagram for screen having 2 web service</strong></figcaption></figure><p>To see complete code you can check out code here</p><p><a href="https://github.com/jagelooyadav/TheCockTail">Here</a></p><p><strong>Conclusion</strong>: Hope this tutorial and code helped you in writing testable code into MVVM and if any assignment/interview you get in MVVM you would crack it. If any confusion and question you have, you can directly comment here or email me on jageloo.yadav@gmail.com Always be there for help. Happy reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=54bffa92dec3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Right way to make reusable UIView component for any design patterns/Architecture (MVC, MVVM, VIPER…]]></title>
            <link>https://medium.com/@jageloo.yadav/right-way-to-make-reusable-uiview-component-for-any-design-patterns-architecture-mvc-mvvm-viper-abcace933853?source=rss-176e0bb80b84------2</link>
            <guid isPermaLink="false">https://medium.com/p/abcace933853</guid>
            <dc:creator><![CDATA[Jageloo Yadav]]></dc:creator>
            <pubDate>Sun, 27 Feb 2022 13:30:26 GMT</pubDate>
            <atom:updated>2022-02-27T13:30:26.473Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Right way to make reusable UIView component for any design patterns/Architecture (MVC, MVVM, VIPER, VIP)</strong></p><p>Every developer has their own perspective to make reusable components. Some developers use Xib to make reusable components or some make programmatically.</p><p>But my personal preference is to make a UIView component programmatically with the correct data binding approach.</p><p>Here are some points I would like to highlight on reusable components which make clean, reusable, independent uiview components.</p><ol><li>Component should be independent and should not dependent on any other module except UIKit, Foundation or any other UI library.</li><li>Component should not have reference of any raw view model , raw model or any object of different module, not UI module object.</li><li>Component should be open to upgrade but closed for modification</li></ol><p>Suppose we need to make below UI components</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*1KLToSl2887lO-Un" /></figure><p><strong>4 // Child control naming</strong></p><p>Child naming should be generic so that can be utilise other modules, apps</p><p>For example. Below naming can be for above UI component</p><p><strong>private</strong> <strong>lazy</strong> <strong>var</strong> titleLabel: UILabel = {</p><p><strong>let</strong> label = UILabel()</p><p>// Set other property on label</p><p><strong>return</strong> label</p><p>}()</p><p><strong>private</strong> <strong>lazy</strong> <strong>var</strong> subTitleLabel: UILabel = {</p><p><strong>let</strong> label = UILabel()</p><p><strong>return</strong> label</p><p>}()</p><p><strong>private</strong> <strong>lazy</strong> <strong>var</strong> detailTitleLabel: UILabel = {</p><p><strong>let</strong> label = UILabel()</p><p><strong>return</strong> label</p><p>}()</p><p><strong>private</strong> <strong>lazy</strong> <strong>var</strong> thumImageView: UIImageView = {</p><p><strong>let</strong> imageView = UIImageView()</p><p>// Set other property</p><p><strong>return</strong> imageView</p><p>}()</p><p>We should not give variable names like <strong>restaurantNameLabel</strong>, <strong>timeLabel</strong>, <strong>locationLabel. </strong>If we give a naming convention like this, components can not be utilised for other purposes even if the same design comes in any other part of the app. You will have to make different components for that purpose which will violate the reusability concept.</p><p><strong>5- Initialisation:</strong></p><p>We can either override superclass or create our own initialiser and call super initialisation within it.</p><p><strong>override init(frame: CGRect) {</strong></p><p><strong>super.init(frame: frame)</strong></p><p><strong>self.setup()</strong></p><p><strong>}</strong></p><p><strong>required init?(coder: NSCoder) {</strong></p><p><strong>fatalError(“init(coder:) has not been implemented”)</strong></p><p><strong>self.setup()</strong></p><p><strong>}</strong></p><p>Where setup() functions as per choice naming. Within this function you can add constraints , add child views .</p><p><strong>6 // Data Binding or Public setter to update UI</strong></p><p>This is the most important section where most of the developers lack. We should follow proper computer basic principles like Abstraction, SOLID, Dependency injection.</p><p><strong>Suppose ViewModel Created for above design is below:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OjRVP2Bg02OsOL9TT8zSGw.png" /></figure><p>below bindings are rejected:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PMFW7bXEZ-4TawFHh632sw.png" /></figure><p>If see above binding, it data tightly coupled with components. It means changing data we have to change our component which is wrong. It also violates dependency inversion principle. Components should not communicate with concrete objects but it should work on its abstraction.</p><p>So here Accepted approach:</p><ul><li>If we have to pass a group of data, we should define an abstraction protocol for component view binding.</li><li>We can create proxy data variable to map with each individual ui element.</li></ul><p>For passing group of data we should take abstraction as below:</p><p><strong>protocol</strong> ItemCardDataSource {</p><p><strong>var</strong> title: String { <strong>get</strong> }</p><p><strong>var</strong> subtitle: String { <strong>get</strong> }</p><p><strong>var</strong> detailTitle: String { <strong>get</strong> }</p><p><strong>var</strong> thumbURLString: String? { <strong>get</strong> }</p><p>}</p><p>Above protocol should be declare in component file itself so that it can be reused across the module.</p><p>So here are the accepted pattern</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PwFjuDUyH19om_5TbTQuGw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BKAzD0Fo5x5fXNUzzMacLg.png" /></figure><p>Once your component is ready, you can pass data as per below approach</p><p>itemCardObject.viewModel = ItemCardViewModel(restaurant: restorantobj)</p><p>For imge loading</p><p><strong>if</strong> <strong>let</strong> urlString = cell.viewModel?.thumbURLString, <strong>let</strong> url = URL(string: urlString) {</p><p>ImageCache.publicCache.load(url: url <strong>as</strong> NSURL) { [<strong>weak</strong> itemCardObject] image <strong>in</strong></p><p>itemCardObject?.loadImage(image: image)</p><p>}</p><p>}</p><p><strong>Conclusion — </strong>I have seen developers make their own feature specific view component even if the designs are the same. We should think and analyse before making any design. There is a possibility that the existing component is already built and you can refactor a little bit or upgrade existing component to achieve your UI and make space for future scaling. Creating your own feature specific view component will unnecessarily add new files and increase code base.</p><p>If you apply the above template, hope your code would be clean and concise and reusable across multiple project modules. If you have any suggestions, comment on the page. I will update this page accordingly as per your suggestion.</p><p>To see complete design for above given component you can checkout code</p><p><a href="https://github.com/jagelooyadav/TheCockTail/blob/main/TheCockTail/TheCockTail/Views/ItemCardCollectionCell.swift">https://github.com/jagelooyadav/TheCockTail/blob/main/TheCockTail/TheCockTail/Views/ItemCardCollectionCell.swift</a></p><p>Happy Reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=abcace933853" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Swift 5.5 Concurrency using Actor, async, await:]]></title>
            <link>https://medium.com/@jageloo.yadav/swift-5-5-concurrency-using-actor-async-dd799899b4b?source=rss-176e0bb80b84------2</link>
            <guid isPermaLink="false">https://medium.com/p/dd799899b4b</guid>
            <category><![CDATA[swift-async-await]]></category>
            <dc:creator><![CDATA[Jageloo Yadav]]></dc:creator>
            <pubDate>Fri, 10 Dec 2021 23:43:00 GMT</pubDate>
            <atom:updated>2021-12-11T07:44:31.799Z</atom:updated>
            <content:encoded><![CDATA[<p>Swift 5.5 introduces new way to handle concurrency in iOS to clean swift code. We will discuss one by one in detail and know how to use these keyword in programming.</p><h3><strong>Actor:</strong></h3><p>Actor is new data type introduced in swift just to tackle data synchronisation.</p><p>Below are the key concepts about actors:</p><ul><li>Actor is reference type</li><li>Actor allow only one thread at a time to access its property ie handle thread synchronisation internally</li><li>To access actors’ property one needs to put await key word before it.</li><li>Actor does not support inheritance but can implement protocol</li><li>Can not mutate actor property outside using accessor (actor.property1 = something not allowed)</li><li>Internally each actor implements “Actor” protocol</li></ul><h3><strong>Why actor came:</strong></h3><p>Actor introduced for just clean our thready safety code which we used to using lock, semaphore, @syncronisation. Below is the example shows why actor is needed:</p><p>Suppose i am writing below completely thread safe code</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DwRkUgTdw7HNc7Izakprtw.png" /></figure><p>Above class is completely thread safe. So accessing localCache and updating local cache in multiple thread will not create any problem. But code looks very dirty and messy so to clean this kind of code, apple introduces “actor”.</p><p>Now using actor same set of code can be written as below which handles thready safety internally.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Gkiym5sieH-tnRvOheEiOA.png" /></figure><p>Above both code is written for same purpose.</p><p><strong>To access class one we need to put below code</strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hJEq2ThFtRc88qjv6WdanQ.png" /></figure><p><strong>But to access actor one just needs to write these lines of code</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oQ4yiMjyGmW3RtAYbgUKag.png" /></figure><p>Await is only used inside function which supports concurrency that means function should be async func.</p><p>If we use await within non async function we will get below error.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fMKYwKuTLg60VJuFcONl2Q.png" /></figure><p>So fix above error we can code like below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/964/1*QM0152zPpeGZxxZTcZ1C4Q.png" /></figure><p>or</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/756/1*4TmtfFYaJ8vNY1z3asrPRA.png" /></figure><h3><strong>Async</strong></h3><p>Async and await are new concept introduced by apple just similar to other languages like java script, c # and kotlin.</p><p>Main motive of async is to provide facility to return long running asynchronous task in one line instead returning by completion block.</p><p>Below are the example why this needed:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1018/1*x2dM-4SEmabgYnkErm6Erg.png" /></figure><p>But if we update function using “async”, caller side would be very clean.</p><p>We can convert existing completion api using async</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*L2VzHjIi4e3ofOtsPqtWpw.png" /></figure><p>Now caller end code would be very clean</p><pre>let searchResults = await searchMovie(name: “Movie name”)</pre><p>here <strong>withCheckedThrowingContinuation</strong> is function which would be utilised to give sync return from async completion block.</p><p><strong>Syntax is</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KLTCdCQPuu1yJGpQPFz74A.png" /></figure><p><strong>Async has below other benefits</strong>:</p><ul><li>It reduces pyramids of completion block. See below example using block</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G9NeGK1lbDdbRHGzJ-TI6Q.png" /></figure><p>but if all api would have used async returns instead completion block</p><p>it could have like below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jfvgLt9m51RRCqFc4UWDoA.png" /></figure><p><strong>Syntax to make async function:</strong></p><p>func functionName() async -&gt; returnType ifAny{</p><p>return calculation</p><p>}</p><p>for example</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pnK2aZuqtuiS5kYVuIlxRw.png" /></figure><p><strong>Syntax to call async function:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tfp7eZIACl9_NiI1VKqmUA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GcrpiAUkBIrDuU_H0RK8hg.png" /></figure><p>Note- I have written one network layer and apiClient using actor, async and Task in one of small app. you can find</p><p><a href="https://github.com/jagelooyadav/SampleFeatruedApps/tree/master/MovieSearch">https://github.com/jagelooyadav/SampleFeatruedApps/tree/master/MovieSearch</a></p><p>Happy reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dd799899b4b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Best way of grouping/arranging members in class/struct in swift]]></title>
            <link>https://medium.com/@jageloo.yadav/best-way-of-grouping-arranging-members-in-class-struct-in-swift-87b7a7c5a572?source=rss-176e0bb80b84------2</link>
            <guid isPermaLink="false">https://medium.com/p/87b7a7c5a572</guid>
            <dc:creator><![CDATA[Jageloo Yadav]]></dc:creator>
            <pubDate>Wed, 09 Oct 2019 18:17:19 GMT</pubDate>
            <atom:updated>2019-10-09T18:17:19.063Z</atom:updated>
            <content:encoded><![CDATA[<p>If you are swift programmer and working for long running product, your each and every file should contain easy readable, easy understood and easy maintainable code. To increase code readability, your code should be proper organised and documented.</p><p>I always follow grouping by either using “MARK” or grouping in extension.</p><p>Sequence can be below</p><p><strong>class</strong> ClassName {</p><p>1-<em>// MARK: — Constants</em></p><p>2-<em>// MARK: — public properties</em></p><p>3-<em>// MARK: — private properties</em></p><p>4-<em>// MARK: — Initialiser</em></p><p>5-<em>// MARK: — life cycle</em></p><p>6-<em>// MARK: — overridden super class method</em></p><p>6-<em>// MARK: public methods/ computed properties</em></p><p>7-<em>// MARK: private methods/computed properties</em></p><p>}</p><p>if you do not want to use “MARK”, you can organise code by swift extension as below:</p><p><strong>1- class</strong> ClassName {</p><p><em>//1- contain all stored public properties in dictionary order</em></p><p><em>//2- contain all stored private properties in dictionary order</em></p><p><em>// 3- initialisers</em></p><p>}</p><p><em>// extension for constants in dictionary/alphabetical order</em></p><p><strong>2- extension</strong> ClassName {</p><p><em>//constant1..</em></p><p><em>//constant2..</em></p><p><em>//constant3..</em></p><p><em>//.</em></p><p><em>//.</em></p><p><em>//constant n</em></p><p>}</p><p><em>// extension for private methods and private computed properties except overridden or protocol methods in dictionary/alphabetical order</em></p><p><strong>3- extension</strong> ClassName {</p><p><em>// private method1</em></p><p><em>// private method2</em></p><p><em>//.</em></p><p><em>//.</em></p><p><em>//.</em></p><p><em>// private methodn</em></p><p>}</p><p><em>// extension for public methods and public computed properties</em></p><p><strong>4- extension</strong> ClassName {</p><p>}</p><p><em>// extension for overridden method or life cycle method in dictionary/alphabetical order</em></p><p><strong>5- extension</strong> ClassName {</p><p>}</p><p><em>// extension for protocol methods in dictionary/alphabetical order</em></p><p><strong>6- extension</strong> ClassName: ProtocolName {</p><p>}</p><p>you can see how i have organised code in OptionButtonView</p><p><a href="https://bitbucket.org/AgrimJageloo/medium/src/master/OptionView/OptionView/OptionButtonView.swift">Bitbucket</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=87b7a7c5a572" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>