<?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 Mukul More on Medium]]></title>
        <description><![CDATA[Stories by Mukul More on Medium]]></description>
        <link>https://medium.com/@mukulmore?source=rss-f6cd09a2fea5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*R3k6GVUTi5Ld184k3-BWgw.jpeg</url>
            <title>Stories by Mukul More on Medium</title>
            <link>https://medium.com/@mukulmore?source=rss-f6cd09a2fea5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 15 Nov 2026 20:05:17 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mukulmore/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[Find iOS Memory Leaks with Spider-Man]]></title>
            <link>https://medium.com/@mukulmore/find-ios-memory-leaks-with-spider-man-c67f224f9a89?source=rss-f6cd09a2fea5------2</link>
            <guid isPermaLink="false">https://medium.com/p/c67f224f9a89</guid>
            <category><![CDATA[objective-c]]></category>
            <category><![CDATA[instruments]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[memory-leak]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Mukul More]]></dc:creator>
            <pubDate>Thu, 09 Jan 2020 16:12:08 GMT</pubDate>
            <atom:updated>2020-01-09T16:12:08.194Z</atom:updated>
            <content:encoded><![CDATA[<p>No fellas! Spider-Man is not a tool or a technique.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/1*ph6Cc_dNJTfjoEHjGDyhJg.gif" /></figure><p><em>Currently, the App Store has about 1.8 million available applications and in this Bundle we all want our application to stand out. </em><strong><em>Don’t we</em>?</strong></p><p><strong><em>Rule number 1</em></strong><em>: Your app should consume</em><strong><em> less memory</em></strong><em>, which would result in </em><strong><em>faster and better</em></strong><em> user experien</em>ce.</p><p><em>Apple provides great tools along with Xcode for profiling the app. <br>Today,we will discuss memory leaks in iOS apps and how to </em><strong><em>find and fix</em></strong><em> them.</em></p><p>Let us begin our story of memory leaks.</p><p>OH! Firstly, let me introduce you to The Protagonist, <strong>Spider-Man,</strong> our iOS developer, who works passionately, striving to achieve the best possible solution.</p><p>One day, Spider-Man was given the task of developing a simple application. Wherein, he had to show a list of images, and upon clicking of any image, the user should navigate to show the image in fullscreen.</p><p>Spiderman, our brilliant developer started coding and built the app within a mere 3 hours. But, Spidey was worried, wondering if he traded quality for speed. Unhappy with the development, as every time he would open the app and navigate to screen of different images, he could see a significant spike in memory.</p><p>When he launched the application he could see the memory usages at<strong> 25MB</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/538/1*TGQpbjPbn5TEDnnDh6Kf8A.png" /><figcaption>Memory stats when the app was launched</figcaption></figure><p>After 2 mins of usage and navigation through different images, the statistics were as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/518/1*srtb9s2r4v5HiaT5667mIg.png" /><figcaption>Memory stats post 2 mins of app usage.</figcaption></figure><p>Spidey was now perplexed, as to what was causing the jump in the memory. But, being a talented developer he knew the glitch is a memory leak, somewhere but didn’t know how to find the exact location of the leak in the project.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*cuo81WWvss1HE67yXytsKg.jpeg" /><figcaption>Spider-man trying to find the memory leak</figcaption></figure><p>Spidey calls his mentor, <strong>IRON MAN. </strong>I<strong> </strong>mean, What is it that Iron Man can’t take care of.<br>Iron Man, suggested him to <strong>use profiling</strong> to find out the memory leak and busily hung up the phone. ( you know where)</p><p>Spidey losing patience as he wanted to solve the leak as soon as possible.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/220/1*rKW9h3eNC8rSpPEv3Cm3gg.png" /><figcaption>Frustrated Spidey</figcaption></figure><p>After 5 mins of panicking, he decides upon finding a solution. He remembered the word “Profiling” used by Iron Man, and started searching for Profiling in Xcode where he found<strong><em> Profile</em></strong> under <em>the</em><strong><em> Product</em></strong> menu. The shortcut to Profile<strong> is Cmd </strong>+ I.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/416/1*l52v-HJiDPE1O0BNN_JvRQ.png" /><figcaption>Profile option in Product (Cmd+I)</figcaption></figure><p>Before we jump to profiling, let’s look at the code written by Spidey.</p><p><strong>Download the starter project from :</strong><br><a href="https://github.com/mukulm24/iOSMemoryLeakDemo">https://github.com/mukulm24/iOSMemoryLeakDemo</a></p><p>Go ahead and run the project and follow the steps :</p><ul><li>Select an avenger from the list of avengers</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*CJdQ9a4z-2HyzNi2gwBgaQ.png" /></figure><ul><li>Click on any item to view the avenger image.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*_qtk_2SWi-0EDk7q3BW2KQ.png" /></figure><ul><li>Repeat these steps for around <strong>40–50 times.</strong></li></ul><p>After doing the above steps, you will observe that the memory usage is increasing continuously, as we saw in the beginning.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/220/1*WoTwCrFaCAnfjDSoGXRqPg.gif" /></figure><p>Coming back to Spidey, he has already found the profile option under the Product menu. He came across the following screen with multiple options</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Hovb-wQBLCzrl8aMFDQXuA.png" /><figcaption>Profiling template chooser</figcaption></figure><p>As Spidey knew there was an issue with memory leak, he decided to select Allocations. <strong>INSTRUMENTS </strong>opened up with the below page</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ln58ILCLyREfQ0kP-5veiQ.png" /></figure><p>The red button on left looks tempting, let’s go ahead and press that.</p><p>Spidey starts using the app as he was testing before.</p><p>Spidey can see a spike in memory and is stuck at the same place, the question of leak locations still persists. <br>He calls up his mentor Iron Man again, and Iron Man replies with “Spidey! Check the CALL TREES option there”</p><blockquote>The ground rule is pop the view controller, and observe the memory allocation. The popped view controller should no longer be listed. If you still see it, then you have a memory leak!</blockquote><p>Spidey goes ahead and finds the call trees under after clicking on Statistics</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R1bNGciefDw0Y3UIF3MLLQ.png" /></figure><p>He sees 26.29 MB (80%) used, goes down the tree to find the exact location by expanding it and following with the highest bytes used.</p><blockquote>Spidey can see many system calls are there which are not useful for the purpose. He decided to hide those calls from a list by checking the option as shown in the below screenshot</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AOyk_KmFjNtXnyVuDmJbQw.png" /></figure><p>There we go, now we are left with only a few traces.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*twV27h9vJkzXIMojMDA-3A.png" /></figure><p>He goes and inspects the elements with most reference counts, and, double click opens up the given file.</p><blockquote>If you now repeatedly push and pop the leaking view controller, then you will see that the <strong>Persistent</strong> will increase. That is a count of the number of that object in memory.</blockquote><p>As the UIImage unit had the problem, We clearly have an idea about where the problem lies.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*R5kWOhiPUjWZnIWG0cEuYA.gif" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/429/1*IXQOSu6Sg50bXm0Iv_uItg.gif" /></figure><p>Completely unexpected, Iron Man is here. Spidey is now looking forward to some advanced profiling.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*CEEwWalEXOzhcjcMkqCDNA.gif" /></figure><p>Iron man appreciates Spidey but also suggests that he needs to find the exact location of the leak. For that move to ‘Statistics’ and search for your view controller whose push/pop is increasing the memory. Spidey goes ahead and searches for ‘AvengersDetailViewController’ and double clicks on it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*swXO3iePO0SXLCUEoH4V-g.png" /></figure><p>In real time, you can go and push/pop the view controllers and see new instances are created and not released, which means that there is a memory leak. On pop of the view, the view controller should get deallocated.</p><p>There were multiple instances created and none of them were released.<br>Finding the exact memory leak reference is difficult as ARC only keeps the count of references and not the information about the referenced objects.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*l1qnldtDfG5Zefw0i--Tyw.png" /></figure><p>Select <em>unpaired</em> for better required result set.<br>Now, Iron Man needs to find where the leak is by eliminating all the UIKit system responsible callers for whom the release is not present. He found it with the responsible caller <em>AvengerDetailViewController.setupView(with:).</em></p><p>This is our responsible function, he has narrowed Spidey’s leak search to the function where the leak is taking place. <br>Now, it’s Spidey’s job to find the leak in the function</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ox0YCgfZLRhykMOL-d1YSA.png" /></figure><p>Spidey looks closely at the function setupView</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_md7XgeY6Th2_9lroVIPcA.png" /></figure><p>He is not sure about the image view and tries and goes to find out the declaration of <em>delegate.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/350/1*UyL1YMo78WX2D29-a-q0ng.png" /></figure><p>There it is, our smarty Spidey has found that delegate is <strong>strong </strong>by default making the strong reference cycle between self (detail view controller) and image view.</p><p>So the delegate needs to be assigned as <strong>weak </strong>to solve the problem of the memory leak.</p><p>But as the variable is declared as <strong>Any?, </strong>it throws an error <br><em>‘weak’ must not be applied to non-class-bound ‘Any’; consider adding a protocol conformance that has a class bound.</em></p><p><em>So Spidey went ahead and made the change:</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/548/1*nkOGpxmNPEli_oherjS1lw.png" /></figure><h3>EUREKA!!!</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/220/1*PLcBco7LpNlTuaErNvo1Zw.gif" /></figure><p>Spidey is now celebrating, doing the Spidey dance. Post changes, the app runs as smooth as his web.He observes the memory graph shows no spike even with continuous usage!</p><h4>Spidey treats Iron Man and makes sure that he profiles every controller after development to eliminate the chances of memory leak.</h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c67f224f9a89" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Swift 4 “CODABLE” magic]]></title>
            <link>https://medium.com/@mukulmore/swift-4-codable-magic-57442c91cbae?source=rss-f6cd09a2fea5------2</link>
            <guid isPermaLink="false">https://medium.com/p/57442c91cbae</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[alamofire]]></category>
            <category><![CDATA[json]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[codable]]></category>
            <dc:creator><![CDATA[Mukul More]]></dc:creator>
            <pubDate>Sun, 11 Mar 2018 07:28:50 GMT</pubDate>
            <atom:updated>2018-03-11T14:06:14.954Z</atom:updated>
            <content:encoded><![CDATA[<p>iOS devs have been struggling with NSCoding for a long time. Creating JSON data from your class objects and objects from JSON data has always been a tedious task but with Swift 4 and the magic of <strong><em>codable</em></strong> protocol everything just seems so easy that you wouldn’t believe it the first time.</p><p><a href="https://developer.apple.com/documentation/swift/codable"><em>Codable</em></a> protocol not just helps you with JSON encoding and decoding but if we look at it from a broader perspective it lets you convert the objects to data and reverse. Later, you can choose how you want your data to be serialized or deserialized, it can be property lists or JSON without worrying about losing the value types.</p><p>Let’s go ahead and dive in to the code and understand how can Codable help us.</p><p>I have been trying to rent an appartment for a long time now. So we’ll revolve around appartments and addresses in our sample codes.</p><p>Let’s create our first model Apartment which will have details about building name it’s rent and address.</p><p>Many people prefer using Class to create models but since I won’t be using inheritance over here and using structs is light weight compared to classes. We will go ahead using struct in the sample. Though using Class would not be a problem; <strong>Codable protocol works with both Struct and Class.</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c0a9135817edf34d088a5e03bcb40fe0/href">https://medium.com/media/c0a9135817edf34d088a5e03bcb40fe0/href</a></iframe><p>As you can see, apartment has a property named address which can be defined as follows.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/88b4c2b43af6686f19ca9809bf5c2232/href">https://medium.com/media/88b4c2b43af6686f19ca9809bf5c2232/href</a></iframe><p>Most of you might have a question in your head that can we create nested JSON objects/arrays after implementing codable. The answer is a big YES. To understand the same we have created a property named address which is another codable implemented struct.</p><h4>THE ENCODER</h4><p>Let’s now go ahead and initialise them</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8b0c34da9b011484a03618bd5e3d43d4/href">https://medium.com/media/8b0c34da9b011484a03618bd5e3d43d4/href</a></iframe><p><strong>With NSCoding you had to write encoding decoding methods, may be create layers to take care of it, all of these were error prone and a tedious task to handle.</strong></p><p>We’ll go ahead and convert the created apartment to data object and encode it to JSON.</p><p>The below function is the crux of converting the object to JSON. Depending on your requirement you can either choose to return JSON String or JSON encoded data.</p><p><strong>You can create the following function as extensions or add it to the struct itself. It completely depends on how you want to design your application.</strong> For better understanding, I have used a generalised function definition.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cfe841651a0c9327521c466d43d799d1/href">https://medium.com/media/cfe841651a0c9327521c466d43d799d1/href</a></iframe><p>We will not get into the error handling part as this blog is more about understanding the use of Codable protocol. If you wish you can make changes in the code and use the try catch block more efficiently.</p><p>We can call the above function as follows on the ‘<strong>oxfordApt</strong>’ object we created above.</p><pre>let jsonAptDetails = convertToJSON(apartmentInfo: oxfordApt)</pre><p>The log for the above is :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2MQTRWS-Dumhpwq2MU48mA.png" /><figcaption>Log printed for print(jsonApt)</figcaption></figure><p><strong>Yes. That’s it</strong>. You have successfully converted your model object to JSON without going through the hassle of encoding or even assigning the values manually.</p><h4>THE DECODER</h4><p>Another thing that we are going to look at is <strong>Decoding</strong> the JSON objects. Previously when codable didn’t exist in our lives we had to manually parse the JSON or use some sort of library to do the task for us but with codable if you design your model in such a way that it’s in sync with JSON used as it’s data source the hard work will be drastically reduced and the code’s performance will be improved.</p><p>Let’s just go ahead and convert a JSON data to it’s corresponding model object.</p><p>For sample purpose’s let’s just create a sample JSON String corresponding to our Apartment struct.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/307bbd3e5eac6c8e1868b02337b3e799/href">https://medium.com/media/307bbd3e5eac6c8e1868b02337b3e799/href</a></iframe><p>Now we’’ll create our decoder function as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e74732a73c306db39924aad1f0b4b5d5/href">https://medium.com/media/e74732a73c306db39924aad1f0b4b5d5/href</a></iframe><p>The above function can be called as follows where aptJSONString is the sample JSON string we created above.</p><pre>let bravuriaApt = self.decodingJSONToApartment(aptJSON: aptJSONString)</pre><p>The log for above :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Hl8umOaaG2s8zExzRL_7Gg.png" /><figcaption>Log after decoding JSON string to Apartment Object</figcaption></figure><p><strong>BINGO</strong>! Another task of converting JSON to it’s corresponding model object has been successfully completed. Again without any hassle of parsing or writing lines of code.</p><p><strong>NOTE</strong>: If you don’t have a JSON string but a JSON data object you can skip the part of converting JSON string to data and directly jump to JSONDeocder(). <br>If Using Alamofire and response is expected in JSON format, we get JSON data as response. Hence we can directly use it instead and not use the conversion of JSON string to data.</p><p><strong>We are done here</strong>! Questions and queries are most welcome. I have shared the link to the GIT project if you want to go ahead and play with values and try it yourself. Go ahead.</p><p>The code is written in a way to make a beginner understand, <strong>I would recommend adding the functions to helper classes or extensions when using in actual project</strong>. This is just a stub to get a feel of how things work with Codable protocol.</p><p><a href="https://github.com/mukulm24/CodableDemo">mukulm24/CodableDemo</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=57442c91cbae" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>