<?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 Pavel Vaitsikhouski on Medium]]></title>
        <description><![CDATA[Stories by Pavel Vaitsikhouski on Medium]]></description>
        <link>https://medium.com/@pavel.vaitsikhouski?source=rss-c8034e60d246------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*p3q7CktoRd2wttez.jpg</url>
            <title>Stories by Pavel Vaitsikhouski on Medium</title>
            <link>https://medium.com/@pavel.vaitsikhouski?source=rss-c8034e60d246------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 02:24:31 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@pavel.vaitsikhouski/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[Let’s Build a Fun “Like” Animation in Swift]]></title>
            <link>https://medium.com/better-programming/lets-build-a-fun-like-animation-in-swift-34fab05f6afb?source=rss-c8034e60d246------2</link>
            <guid isPermaLink="false">https://medium.com/p/34fab05f6afb</guid>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[animation]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[core-animation]]></category>
            <dc:creator><![CDATA[Pavel Vaitsikhouski]]></dc:creator>
            <pubDate>Wed, 20 May 2020 11:19:52 GMT</pubDate>
            <atom:updated>2020-05-20T13:58:12.932Z</atom:updated>
            <content:encoded><![CDATA[<h4>A heart animation to make your users happy</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Un7Zkbk-t0I_d_8xZ5L2rg.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@sortino?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Joshua Sortino</a> on <a href="https://unsplash.com/s/photos/future?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>If you like something and want to show it to others you need a like view. If you want to do it beautifully, you need a like view with an explosive animation!</p><p>First, let’s take a look at the animation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*DUoX1JnQao_O9ArzALuf1g.gif" /></figure><p>If you don’t want to read full article, you can find a source code in my <a href="https://github.com/VPavelDm/FavoriteViewAnimation">GitHub repository</a>.</p><p>To understand what the animation is, let’s take a look at the animation in slow motion.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/215/1*L7XbGvLXPriE-6flV-znPA.gif" /></figure><p>As you can see, it consists of four elements:</p><ul><li>The heart (empty at the beginning and full at the end of the animation)</li><li>A red circle that grows.</li><li>A white circle that grows into the red circle.</li><li>Fireworks.</li></ul><p>The animation has several steps:</p><ol><li>Fade out the heart.</li><li>Grow a red circle.</li><li>Grow a white circle.</li><li>Fade in the heart view.</li><li>Blow up fireworks.</li></ol><p>To create these elements and achieve this animation we’ll use Core Animation.</p><p>You need to know what <a href="https://developer.apple.com/documentation/quartzcore/calayer">CALayer</a>, <a href="https://developer.apple.com/documentation/quartzcore/cashapelayer">CAShapeLayer</a>, <a href="https://developer.apple.com/documentation/quartzcore/careplicatorlayer">CAReplicatorLayer</a>, <a href="https://developer.apple.com/documentation/quartzcore/cabasicanimation">CABasicAnimation</a>, <a href="https://developer.apple.com/documentation/quartzcore/cakeyframeanimation">CAKeyframeAnimation</a> and <a href="https://developer.apple.com/documentation/quartzcore/caanimationgroup">CAAnimationGroup</a> are. I won’t be explaining these layers and animations. If you want to know more, read some articles and documentation, and come back to this article when you’re done.</p><p>Now, let’s create our layers. First, we need to create a full and empty like (heart) icon . To create these likes, we need to add icons to our assets.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/466/1*NS56OqT3HC7IFKZOiWY3_w.png" /></figure><p>After that we can create the like layer.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ff5bdb90ecfd5706b511b5b2f765bc62/href">https://medium.com/media/ff5bdb90ecfd5706b511b5b2f765bc62/href</a></iframe><p>As you can see, we write the <em>changeLikeState(isFilled:)</em> method. This either adds a filled like or not to the layer depending on the isFilled parameter. Note that we use <em>mask property</em>, so we can<em> </em>change the icon color.</p><p>Let’s move on. Now, we’ll create circles.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b3b517607c52c186cd2ac1522d8dac59/href">https://medium.com/media/b3b517607c52c186cd2ac1522d8dac59/href</a></iframe><p>At first, we write the createCircleLayer(with:)<em> </em>method. This creates CAShapeLayer and a circle using UIBezierPath, which is added to the path property of the layer. In the end, we use this method to create the redCircleLayer<em> </em>and<em> </em>whiteCircleLayer.</p><p>We’ve done the simplest part. Now, we need to create the firework. Let’s take a look at that.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/248/1*tbmeF6r9ic2cXFEBNnVdOA.png" /></figure><p>The fireworks consist of large and small circles. To create a circle, let’s write a method createDotLayer(radius:)<em>. </em>This method has a radius parameter that defines the size of a circle.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/45dd03a902b22680a2b3d929841a0767/href">https://medium.com/media/45dd03a902b22680a2b3d929841a0767/href</a></iframe><p>Next, we need to create an array of circles. This array consists of two circles: small and big. Then we’ll multiply them using CAReplicatorLayer.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e26e28787e94f568b2dc8df558a3b0fe/href">https://medium.com/media/e26e28787e94f568b2dc8df558a3b0fe/href</a></iframe><p>So, the movableDotShapeLayers<em> </em>property contains two circles: small and big. Next, we need to create the CAReplicatorLayer. To do this we write the createDotsLayer method.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d43bfdec5bd3c80b6de717f18fa5a56e/href">https://medium.com/media/d43bfdec5bd3c80b6de717f18fa5a56e/href</a></iframe><p>So, we’ve created the CAReplicatorLayer and add the small and big circles as a sub-layer. Next, we use the instanceCount property to determine how many circles there will be in the layer. We calculate the distance between them and store that in an angle variable. To stretch the circles we use the instanceTransform property of the layer. To give the circles different colors, we use the instanceBlueOffset property. There are two more properties that we can use, instanceRedOffset<em> </em>and<em> </em>instanceGreenOffset<em>, </em>but I only use instanceBlueOffset.</p><p><em>Let’s look at what we have.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/142/1*FcS6R-t1dMH8zadoYkpddg.png" /></figure><p>Now our circles are under each other (the small circles are under the large ones). Let’s fix that. To do this we add transform for the CAReplicatorLayer. It’s pretty easy.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b545310b1883aaba8b2f3b7e1c4a789e/href">https://medium.com/media/b545310b1883aaba8b2f3b7e1c4a789e/href</a></iframe><p>The result will look like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/154/1*0lG-zsj6GjqzBAw4Ru4c8g.png" /></figure><p>At the start, our circles must be hidden. So, add <em>isHidden = true </em>for the<em> </em>CAReplicatorLayers<em> </em>and move on.</p><p>At this moment, we’ve created all the layers. All we need to do is add tap gestures and some animations.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1ebbe2b5057b3906da1fb2b14ae4d8de/href">https://medium.com/media/1ebbe2b5057b3906da1fb2b14ae4d8de/href</a></iframe><p>If the like view is filled we stop the animation and if not we start the animation. Pretty simple, isn’t it?</p><p>To stop animations let’s write a method, stopAnimations()<em>.</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f6bdaa060a6561956e43c4c19d148172/href">https://medium.com/media/f6bdaa060a6561956e43c4c19d148172/href</a></iframe><p>So, to stop animations all we need to do is remove all the animations from all the layers that can be animated. We also hide unnecessary layers (the red and white circles and fireworks).</p><p>Let’s move on to the first step of the animation. Our first animation fades out the like view. To achieve this effect we animate the opacity of the layer from 1 to 0.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a3521a7c23c8b3afb9976fe0c26fe22e/href">https://medium.com/media/a3521a7c23c8b3afb9976fe0c26fe22e/href</a></iframe><p>Note that we set a new value and use the delegate property. We do this to understand when the animation is stopped. We need it to re-start at the next step of the animation.</p><p>Let’s get ahead of ourselves and take a look at the animation call sequence.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a13de5abf520d1ed982907ba2da9da6b/href">https://medium.com/media/a13de5abf520d1ed982907ba2da9da6b/href</a></iframe><p>animationDidStart(<strong>_</strong>:)<em> </em>— is a delegate method that is called before starting the animation. We use this delegate method to make the necessary layer visible.</p><p>animationDidStop(<strong>_</strong>:, finished:) — is a delegate method that’s called after the animation finishes. We use this delegate method to start the next step of our explosive animation and to hide unnecessary layers.</p><p>As we can see, the sequence of animations is:</p><ol><li>Fade out like view</li><li>Fade in red circle</li><li>Fade in white circle</li><li>Fade in like view</li><li>Make fireworks</li></ol><p>Our last task is to implement these methods. We’ve already implemented the fadeOutLikeView()<em> </em>method. Let’s implement the fadeInRedCircle<em>()</em> method.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/621c0752e0bc9f512f63d9ff78752a01/href">https://medium.com/media/621c0752e0bc9f512f63d9ff78752a01/href</a></iframe><p>Making the red circle appear is very simple — we just increase the scale of the circle from 0 to 1. The same goes for the white circle and like view.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f65e4639e056765ae79034d44d7fdfdd/href">https://medium.com/media/f65e4639e056765ae79034d44d7fdfdd/href</a></iframe><p>We’ve done the simplest part. Let’s implement the last animation.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c860dcea6e7e44f5e1c166bcc1899382/href">https://medium.com/media/c860dcea6e7e44f5e1c166bcc1899382/href</a></iframe><p>This consists of three steps:</p><ul><li>Changing opacity from 1 to 0.</li><li>Changing the scale from 1 to 0.</li><li>Changing position from the current position.</li></ul><p>Opacity and scale animations look the same as before. To implement the position animation we calculate the offset and define nextPosition. Then we combine these animations using the CAAnimationGroup.</p><p>Note that animations are applied to the small and large circles but <em>not</em> to the replicator layer.</p><p>That’s all for today. We’ve implemented a beautiful animation that you can use in your projects. You can find the full code in this <a href="https://github.com/VPavelDm/FavoriteViewAnimation">repository</a>.</p><p>Thanks for reading! 😉</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=34fab05f6afb" width="1" height="1" alt=""><hr><p><a href="https://medium.com/better-programming/lets-build-a-fun-like-animation-in-swift-34fab05f6afb">Let’s Build a Fun “Like” Animation in Swift</a> was originally published in <a href="https://betterprogramming.pub/">Better Programming</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Build an iOS Activity Indicator in the Form of a Snake]]></title>
            <link>https://medium.com/better-programming/ios-activity-indicator-in-the-form-of-a-snake-7dd85c1baec9?source=rss-c8034e60d246------2</link>
            <guid isPermaLink="false">https://medium.com/p/7dd85c1baec9</guid>
            <category><![CDATA[swiftui]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Pavel Vaitsikhouski]]></dc:creator>
            <pubDate>Fri, 17 Apr 2020 20:12:23 GMT</pubDate>
            <atom:updated>2020-04-22T14:27:36.685Z</atom:updated>
            <content:encoded><![CDATA[<h4>How to make your Activity Indicator different from all the others using SwiftUI</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nGQ39goAX9yFLn0byNkGTA.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@jeffgry?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Oğuzhan Akdoğan</a> on <a href="https://unsplash.com/s/photos/loading-screen?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><h3>Introduction</h3><p>Over the past few years, developers have been using activity indicators to let users know that they need to wait for the app to do something. To brighten up that time and stand out from others, developers customize their activity indicators. In this article, we’ll create our own activity indicator using SwiftUI.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/404/1*5dEYwuZaIVXSdJb0_-7SMg.gif" /><figcaption>Activity Indicator</figcaption></figure><h3>Implementation</h3><p>This task can be divided into two subtasks:</p><ul><li>Creating a shape for activity indicator.</li><li>Adding animation.</li></ul><p>Let’s start with creating the shape. To do this, create an ActivityIndicatorShape structure and conform it to the Shape protocol:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a08c06c1f8a190e92c9caa548be9d383/href">https://medium.com/media/a08c06c1f8a190e92c9caa548be9d383/href</a></iframe><p>The path(in: CGRect) -&gt; Path function is necessary to describe the shape. This is the required function for the conformation to the Shape protocol.</p><p>To draw our shape, we will split it into five lines, as shown below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/157/1*DlsKtYx679amYA2kpr90eA.png" /></figure><p>The black lines around the activity indicator are the border of our view. <br>The length of each of the five lines is equal to the width of the view divided by five. Then you can initialize the starting points of each line, as well as the sixth point, which is the end of the fifth line.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/de9c68ebadf5f2d2acbf4ee02c8b679b/href">https://medium.com/media/de9c68ebadf5f2d2acbf4ee02c8b679b/href</a></iframe><p>Next, we connect these lines, making the line width ten, and add rounding:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8a1d6b3846c81019567786b675c013b6/href">https://medium.com/media/8a1d6b3846c81019567786b675c013b6/href</a></iframe><p>Our shape takes the following form:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/194/1*vNxRp30pQJGdP5A5p68UpQ.png" /><figcaption>ActivityIndicator</figcaption></figure><p>You may notice that the edges of some lines do not look exactly as we need them to. This is because the view cuts off everything that goes beyond it. It turns out that we need to reduce the line on the left, top, right and bottom by the size of the radius of the rounding. This radius is equal to half of the line width, in our case five. Thus our code takes the following form:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c19b5548d5cc9950fdb38bd85a869ee0/href">https://medium.com/media/c19b5548d5cc9950fdb38bd85a869ee0/href</a></iframe><p>The shape now looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/180/1*2PqXnvtY0cQpvo78XdHCxA.png" /><figcaption>Activity Indicator Shape</figcaption></figure><p>After the successful implementation of the first part, we proceed to the animation.</p><p>There are many ways to implement this animation. I will describe one of them.</p><p>At first, we draw two ActivityIndicators under each other and give them different colors:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1b813ad570babf944e7a8b97d84cb9fa/href">https://medium.com/media/1b813ad570babf944e7a8b97d84cb9fa/href</a></iframe><p>So we draw one activity indicator, which looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/218/1*5ZTcuK3_A10KPBrwB1msnw.png" /></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d4605e65b3bdf8bcceb53c9ace7bbbed/href">https://medium.com/media/d4605e65b3bdf8bcceb53c9ace7bbbed/href</a></iframe><p>Then we draw the second activity indicator under the first. Our activity indicators will look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/234/1*eTWIc5okIyjU-dJs_3xrOw.png" /></figure><p>Note that the activity indicator’s color has changed slightly. This is because we see the second one with a different color and the first one is on a layer underneath it.</p><p>Next, we need to restrict the area that’s visible for the second activity indicator:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/54ec3745d673a40ba7b8f4fb914f8290/href">https://medium.com/media/54ec3745d673a40ba7b8f4fb914f8290/href</a></iframe><p>The result looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/170/1*j0Ok50VMPodtDYHaxI6hkQ.png" /><figcaption>Activity Indicator</figcaption></figure><p>We’ve learned how to display part of the second activity indicator. All we have to do is animate the visible part. This can be achieved as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5c489b62910a02561421187322e4ccc0/href">https://medium.com/media/5c489b62910a02561421187322e4ccc0/href</a></iframe><p>We’ve created struct Animation, added repeatForever and passed it in func animation(_ animation: Animation?)<strong><em> -&gt; </em></strong>some View<strong><em>. <br></em></strong>The movement of the visible part was implemented using offset.</p><h3>Conclusions</h3><p>In this tutorial, we looked at a way to create your own activity indicator. It may seem complicated at first glance, but in fact, it is not!</p><p>The full source code of this activity indicator using SwiftUI is available in this GitHub <a href="https://github.com/VPavelDm/ProgressBar">Repository</a>.</p><p>I hope you enjoyed reading this tutorial.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7dd85c1baec9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/better-programming/ios-activity-indicator-in-the-form-of-a-snake-7dd85c1baec9">Build an iOS Activity Indicator in the Form of a Snake</a> was originally published in <a href="https://betterprogramming.pub/">Better Programming</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Memory management magic when working with RxSwift]]></title>
            <link>https://medium.com/kufar-tech/memory-management-magic-when-working-with-rxswift-1f09f937179e?source=rss-c8034e60d246------2</link>
            <guid isPermaLink="false">https://medium.com/p/1f09f937179e</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[memory-leak]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[rxswift]]></category>
            <category><![CDATA[weakreference]]></category>
            <dc:creator><![CDATA[Pavel Vaitsikhouski]]></dc:creator>
            <pubDate>Fri, 13 Mar 2020 09:00:51 GMT</pubDate>
            <atom:updated>2020-03-13T09:00:51.251Z</atom:updated>
            <content:encoded><![CDATA[<p>How to stop using weak and unowned references when working with RxSwift</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*YWlmltERf3ruGiwhiIKQNA.jpeg" /></figure><p>This article is intended for people who use RxSwift in their projects and want to better understand how to deal with memory leaks, as well as understand when you can use strong references and when not.</p><p>First let’s remember what ARC is. ARC (Automatic Reference Counting). Swift uses ARC for memory management. Every time you want to create a new instance of a class, ARC allocates new chunk of memory that will be used to store some information about this object. Because the system is limited in resources, you must free up unused memory. To determine whether memory is being used or not, there is a table that stores the number of strong references to each allocated chunk of memory. Whenever you assign a class instance to a property, that property makes a strong reference to the instance and a counter for this chunk of memory is incremented by one. And every time you break down the property, counter is decremented by one. So, if there are no strong references to the instance, memory will be deallocated. This method has one drawback: if 2 instances refer to each other, there will be a memory leak, since counter can never be set to 0. It’s also called as strong reference cycle.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/824/1*S7eRV6OUOXmzql5ua4wsGA.png" /></figure><p>You can get a memory leak in a more unexpected way: let’s say A refers to B, B refers to C, C refers to A. Thus, a chain of these 3 elements will always exist.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/872/1*kXLxgcYc4y2Q9ADXN23mOw.png" /></figure><p>To solve this problem, you can make those references weak. Weak references do not increment the counter by 1. <br>More information can be found <a href="https://docs.swift.org/swift-book/LanguageGuide/AutomaticReferenceCounting.html">here</a>.</p><p>So, we figured out how to deal with memory leak. Let’s look at how memory leaks are they formed when working with RxSwift, as well as when it is necessary and when it is not necessary to use weak references.</p><p>First, let’s look at the simplest example. <br>We all know that we need to store a strong reference to a Disposable and call the <strong><em>dispose</em></strong> method if the class that stores the reference to the Disposable ends up working. Let’s see why this is so. <br>Consider the following code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/22cdeefc5311087e824cabe01f4ff447/href">https://medium.com/media/22cdeefc5311087e824cabe01f4ff447/href</a></iframe><p>Now let’s draw instance’s dependencies diagram during the execution of the <strong><em>createObservable</em></strong> method.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FX_SsdgV3KEp9lvoCbpSSA.png" /></figure><p>After this method’s execution, the Presenter loses the strong reference on Observable, but since Observable and Disposable refer to each other, their strong references counters will not be 0, so the memory will not be freed. In order to free up memory, you must destroy one of the references. To do so, the Observable must stop working and send <em>a completion</em> <em>event</em> or <em>an error</em>. In our example, Observable will never send either <em>the completion event </em>or<em> an error (</em>because <strong><em>repeatElement</em></strong> method is infinite<em>)</em>, so the memory will never be released. The second way to free up memory is to call the <strong><em>dispose</em></strong> method on Disposable. Let’s rewrite our example a bit.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/662b6ef2cfa3f134726450cc862b8990/href">https://medium.com/media/662b6ef2cfa3f134726450cc862b8990/href</a></iframe><p>Since there can be many Observables within one class, saving Disposables and then calling the <strong><em>dispose</em></strong> method for each one is not very convenient and extensible. To solve the extensibility problem, the DisposeBag class was developed, which is needed for storing Disposables and calling <strong><em>dispose</em></strong> method in <strong><em>deinit</em></strong> method for each of them. The following piece of code shows how to work with DisposeBag.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5a84465e983b035b117379387ec7e37f/href">https://medium.com/media/5a84465e983b035b117379387ec7e37f/href</a></iframe><p>Here’s what instance’s dependencies diagram look like.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hU4b9O3PNgZrq3FmmmVMmQ.png" /></figure><p>The diagram shows that if the reference from View to Presenter is lost, the strong references counter for Presenter will be set to 0, and memory will be freed accordingly. Then the memory occupied by the DisposeBag will also be freed, which will cause <strong><em>dispose</em></strong> method to be called from Disposable. This way there will be no memory leaks.</p><p>The next step is to analyze how to work with operators in RxSwift. Suppose we want to store a value that is emitted by Observable.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5d44b1ffbfe8293116d59a41ab0ec8da/href">https://medium.com/media/5d44b1ffbfe8293116d59a41ab0ec8da/href</a></iframe><p>For this case, the dependency diagram looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GT_vPyDpExXisbVEnj6vDA.png" /></figure><p>There is a strong reference between Observable and Presenter, because in the <strong><em>subscribe</em></strong> method Observable refers to `self`.<br>Also in this image, you can see that we have a new way to get memory leak. Since deleting the reference between View and Presenter will not reset the counter for Presenter, the memory of the Presenter will not be freed, so these 4 objects will be stored in memory infinitely. <br>The solution to this problem is to use weak reference.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5601a4c722b2b328bd00053e10644785/href">https://medium.com/media/5601a4c722b2b328bd00053e10644785/href</a></iframe><p>For this case, the dependency diagram looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*i1RHZWdczeh2UgN1pcvDYg.png" /></figure><p>The red arrow indicates a weak reference. A weak reference does not increment the reference counter by 1. Accordingly, when the View loses its reference to Presenter, the memory for Presenter, DisposeBag, Disposable, and Observable will be freed as explained earlier in this article.</p><p>Using weak creates some difficulties when working with properties, since it becomes necessary to handle Optional. Let’s look at all the cases where using weak references is unnecessary.</p><p>Let’s look at the following example. Let’s move the creation of Observable and the greeting property to the Interactor class.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2e568d5772ad3858632963c1611421b3/href">https://medium.com/media/2e568d5772ad3858632963c1611421b3/href</a></iframe><p>The dependency diagram for this example looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mPdtzdq8cY2RWlRX-kAITQ.png" /></figure><p>From this diagram, you can see that if you lose a strong reference between View and Presenter, Presenter will free up memory, and then the memory occupied by DisposeBag, Disposable, Observable will also be freed. Since Observable frees up memory, the strong reference between Observable and Interactor will be deleted, as well as the strong reference between Presenter and Interactor, so the memory occupied by Interactor will be freed. It’s some kind of magic, isn’t it? We were able to use strong references and didn’t get a memory leak. Let’s look at more examples where we can do something similar.</p><p>Let’s get back to the creation of Observable in Presenter. And also create a Mapper class that will convert the string to uppercased. There are several ways to interact with RxSwift operators and properties without using weak references. Let’s look at few of them:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a92b8c25c3b00dda98109d7b1e65e48f/href">https://medium.com/media/a92b8c25c3b00dda98109d7b1e65e48f/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4c69603388fe1c3fc6806c855742ee70/href">https://medium.com/media/4c69603388fe1c3fc6806c855742ee70/href</a></iframe><p>For these two examples, the dependency diagram will look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n-JhS6Cz3l6r108BPLYMrA.png" /></figure><p>This diagram shows that there will be no memory leak. The reason is the same as in the example with Interactor.</p><p>But be careful about passing functions to operators. If we try to pass the Presenter function, there will be a memory leak. Let’s look at this example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9a880aa4c4802af026295d5eee65084a/href">https://medium.com/media/9a880aa4c4802af026295d5eee65084a/href</a></iframe><p>The dependency diagram will look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7LMf8mqexlDTqM_IECfY1w.png" /></figure><p>Unfortunately, in this case, there is a memory leak, and there is only one way to fix it. You can do it using weak references.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8e42666d4a96438720329364615363f9/href">https://medium.com/media/8e42666d4a96438720329364615363f9/href</a></iframe><p>Moreover, in this case, it is necessary to process Optional, which we are trying to avoid.</p><p>I would like to note that if the Mapper is a value type, the dependency diagram will look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iNL_M0Xr88-i991iecrhyA.png" /></figure><p>And the code will look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/def2bab66a84480f8cdedf71cf8427aa/href">https://medium.com/media/def2bab66a84480f8cdedf71cf8427aa/href</a></iframe><p>You can see from this figure that you can copy value type variables without fear of getting a leak memory. <br>More information about value and reference types can be found <a href="https://docs.swift.org/swift-book/LanguageGuide/ClassesAndStructures.html">here</a>.</p><p>Thanks for reading, please share it if you found it useful :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1f09f937179e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/kufar-tech/memory-management-magic-when-working-with-rxswift-1f09f937179e">Memory management magic when working with RxSwift</a> was originally published in <a href="https://medium.com/kufar-tech">Kufar Tech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>