<?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 Jaafar Barek on Medium]]></title>
        <description><![CDATA[Stories by Jaafar Barek on Medium]]></description>
        <link>https://medium.com/@BarekJaafar?source=rss-4a8462234f26------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*POxzQ5RWilhyYyK1vEgcpA@2x.jpeg</url>
            <title>Stories by Jaafar Barek on Medium</title>
            <link>https://medium.com/@BarekJaafar?source=rss-4a8462234f26------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 23:09:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@BarekJaafar/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[Swift Weak vs Unowned]]></title>
            <link>https://medium.com/hackernoon/swift-weak-vs-unowned-by-examples-ffcc7c25ecc8?source=rss-4a8462234f26------2</link>
            <guid isPermaLink="false">https://medium.com/p/ffcc7c25ecc8</guid>
            <category><![CDATA[unowned]]></category>
            <category><![CDATA[weak]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[memory-management]]></category>
            <dc:creator><![CDATA[Jaafar Barek]]></dc:creator>
            <pubDate>Thu, 23 May 2019 12:04:06 GMT</pubDate>
            <atom:updated>2019-05-27T04:46:12.495Z</atom:updated>
            <content:encoded><![CDATA[<h4>When to use Weak and when to use Unowned, by examples.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*A6VUYaIQqOjUicOtyAyd8Q.jpeg" /></figure><h4>Problem</h4><p>In a <a href="https://hackernoon.com/swift-avoiding-memory-leaks-by-examples-f901883d96e5">previous article</a>, we discussed how Automatic Reference Counting (ARC) is used to manage memory usage in an iOS application. However, in some cases, ARC is not able to release objects from memory due to retain cycles.</p><p>Retain Cycle is the case when 2 objects have a strong reference to each other and are retained, make it impossible for ARC to release those object from the memory and cause what we call a “Memory Leak”.</p><h4>Solution</h4><p>By default whenever we declare a variable it is considered as “strong’’, <br>to avoid retain cycles we should declare those variable as “weak” or “unowned”. This article main focus is not about handling memory management, if you are not familiar with that topic read the <a href="https://hackernoon.com/swift-avoiding-memory-leaks-by-examples-f901883d96e5">previous article.</a></p><p>Our focus will be on when to use weak and when it is better to use unowned by real-world examples.</p><h3>Weak vs Unowned</h3><p><a href="https://docs.swift.org/swift-book/LanguageGuide/AutomaticReferenceCounting.html">According to Apple’s Documentation:</a></p><blockquote>Define a capture in a closure as an unowned reference when the closure and the instance it captures will always refer to each other, and will always be deallocated at the same time.</blockquote><blockquote>Conversely, define a capture as a weak reference when the captured reference may become nil at some point in the future. Weak references are always of an optional type, and automatically become nil when the instance they reference is deallocated. This enables you to check for their existence within the closure’s body.</blockquote><p>The main difference between weak and unowned is that weak is optional while unowned is non-optional. <br>By declaring it weak you get to handle the case that it might be nil inside the closure at some point. If you try to access an unowned variable that happens to be nil, it will crash the whole program. So only use unowned when you are positive that variable will always be around while the closure is around.</p><h4><strong>Unowned</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WOKexWxKDY5dXmjE_Tna9w.png" /></figure><p>You might have heard the above a lot before, but when you should really be positive that the variable will always be around when the closure is around so you can use unowned without any worries?<br>Let us see the example below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/65c2c65326565321356b18df0913d610/href">https://medium.com/media/65c2c65326565321356b18df0913d610/href</a></iframe><p>In the example above, there is no way that the closure will live more than the ViewController, in that case, you can be confident to use unowned.</p><h4><strong>Weak</strong></h4><p>Now in the following example, let us see when you should use weak to avoid crashes:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/495dfafdbbbfba6b37c66ab36cd2552c/href">https://medium.com/media/495dfafdbbbfba6b37c66ab36cd2552c/href</a></iframe><p>In the above case, you can never be sure that the ViewController will live more than the closure since the network call response can occur after closing the controller, so you should definitely use weak as an optional to avoid crashes.</p><p>If we can use weak in all cases, then why to use unowned and take a risk of the app crashing? The answer is simple, according to apple:</p><blockquote>If the captured reference will never become nil, it should always be captured as an unowned reference, rather than a weak reference.</blockquote><p>Unowned is faster and allows for immutability and non-optionality.</p><p><strong>If you don’t need weak, don’t use it.</strong></p><h4>Conclusion</h4><p>To avoid memory leaks we should declare variables as weak or unowned instead of the default implementation which is strong.<br>However the key difference between them is that weak is an optional type , while unowned is not, so we should only use unowned when we are 100% sure that the closure have the same lifetime of the variable, so the closure will be reachable <em>only until the variable is reachable</em>.</p><h4>References</h4><ul><li><a href="https://docs.swift.org/swift-book/LanguageGuide/AutomaticReferenceCounting.html">Apple’s Documentation</a></li><li><a href="https://stackoverflow.com/questions/24320347/shall-we-always-use-unowned-self-inside-closure-in-swift">Stackoverflow</a></li></ul><p>Read my previous article about Avoiding Memory Leaks by Examples</p><p><a href="https://hackernoon.com/swift-avoiding-memory-leaks-by-examples-f901883d96e5">Swift: Avoiding Memory Leaks by Examples</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ffcc7c25ecc8" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackernoon/swift-weak-vs-unowned-by-examples-ffcc7c25ecc8">Swift Weak vs Unowned</a> was originally published in <a href="https://medium.com/hackernoon">HackerNoon.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[6 iOS Applications Tips For a Better User Experience]]></title>
            <link>https://medium.com/hackernoon/6-ios-applications-tips-for-a-better-user-experience-67d172853491?source=rss-4a8462234f26------2</link>
            <guid isPermaLink="false">https://medium.com/p/67d172853491</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[design]]></category>
            <category><![CDATA[ux]]></category>
            <category><![CDATA[ios-ux]]></category>
            <dc:creator><![CDATA[Jaafar Barek]]></dc:creator>
            <pubDate>Sat, 26 Jan 2019 12:55:58 GMT</pubDate>
            <atom:updated>2019-02-01T14:08:48.718Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ud7JwDc-grF4baKsrEHb0g.jpeg" /></figure><p>Whether you are an experienced mobile application developer or you just started your career in this field, user experience while using your application is so important . Believe it or not , even if you spent months or even years working on the functionality of your application users wouldn’t be merciful when it comes to the performance or look of your app , in the best scenario they will delete the app or stop using it or your app ending with a 1 star review on the apple store.</p><p>Let us discover together in this article , 6 simple tips that can take your application’s performance and user experience to the next level.</p><h3>1- Loading Indicator While Fetching Data:</h3><p>Almost in any application the user needs to wait for data fetching. Let us suppose that you have a simple login screen where the users fills his credentials and press on the login button so you have to call a certain API and wait for the data to get back to you..</p><p>The worst thing you can do in this case is not displaying any indicator for the user that a certain operation is occurring right now. Before you even do anything you already gave a bad impression to the user.</p><p>A lot of developers will use an indicator that will cover half of the screen and block the UI like this :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*bF6kFBmMgeqrySzFDbWL8Q.png" /></figure><p>That is not wrong , but my personal approach is using a lighter way as below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*dkpkDgGr5gPqh3TN35m1MA.gif" /></figure><p>It is up to you which indicator you use the important thing is for it to be user friendly , and to use it whenever needed.</p><h3>2- Animations:</h3><p>Although you can write a complete application without any animation , it is so important to add some to make your application go to the next level and give the user a better experience.</p><p>We will not dive into animations of course because that is a huge topic to cover in an article , but I will give you a small example from medium application:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*3G-NGusF3CO7_kkNnAs7iA.gif" /></figure><p>Notice how when the user attempt to click on an item in the collection view , a simple yet very nice animation appears.</p><p>To Achieve a similar animation you can simply add the below code to your controller:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d5240c53c2b3b8968eaee14076b97d51/href">https://medium.com/media/d5240c53c2b3b8968eaee14076b97d51/href</a></iframe><p>To dive more into animations I recommend <a href="http://twitter.com/rwenderlich">@rwenderlich</a> <a href="https://store.raywenderlich.com/products/ios-animations-by-tutorials">Raywenderlich Animations by Tutorials Book</a> and <a href="https://www.youtube.com/channel/UCuP2vJ6kRutQBfRmdcI92mA">Let us build that app channel on youtube</a>.</p><h3>3- Watch Out For Memory Leaks:</h3><blockquote><em>Memory leaks are dangerous in you app because they will affect your app’s performance and might lead to crashes when the app runs out of memory.</em></blockquote><p>For a good user experience and application performance you need to do a good job when it comes to dealing with memory management.<br>Automatic Reference Counter (ARC) does a great job automatically for you , but you need to be aware about 3 commons case that may lead to leaks , check out my <a href="https://hackernoon.com/swift-avoiding-memory-leaks-by-examples-f901883d96e5">article</a> were we talk about memory management by examples.</p><h3>4- Never Block the Main Thread:</h3><p>Whenever you need to perform any heavy computation in your application you must not do it on the main thread which should be kept for updating UI only. In that way your application will run in a smooth asynchronous manner.<br>To learn more about Multi Threading in Swift check out my article:<br><a href="https://hackernoon.com/swift-multi-threading-using-gcd-for-beginners-2581b7aa21cb">Swift Multi-Threading using GCD For Beginners.</a></p><h3>5- Caching:</h3><p>In almost any application you need to get data from an online server , and most likely download images to display to the user.<br>Having the user wait every time to fetch the same image or data he previously downloaded is a very bad experience.<br>You as a developer have the decision to cache whatever data you find a necessity to do so , but at least you should cache image downloading and that can be easily done using third party framework like <a href="https://github.com/SDWebImage/SDWebImage">SDWebImage</a> or <a href="https://github.com/onevcat/Kingfisher">KingFisher</a>.</p><h3>6- Design:</h3><p>Even if you had an incredible functionality in your application , if you did not express that to the user in a beautiful design you will have a bad review for your app and users might stop using it.</p><p>Replicating the design can be tricky , you often might have pixelated images , or few pixels shifted from the original design without noticing.<br>Even some colors you might thought you have them the same but sadly you didn’t. You need to spend more time on checking those aspects.</p><p>As an iOS developer I cannot be more thankful to <a href="https://medium.com/u/f5816dcbf13f">Flawless App</a> for the amazing tool that helped me to compare the expected design and the real app right inside iOS Simulator. Ever since I have been using it I was able to be confident that I replicated the design exactly as intended by the designer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*idB4VvzbZ3kYCA8oqQhJMw.png" /><figcaption><a href="https://flawlessapp.io/">https://flawlessapp.io/</a></figcaption></figure><h3>Final Thoughts:</h3><p>Delivering a great user experience in mobile applications can be tricky some times , however with some simple tips you can improve any applications ‘ performance and experience and make sure users will fall in love with your app.</p><p>If you have any comments or more tips you are more than welcomed to comment , or contact me <a href="http://jaafarbarek@gmail.com/">here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=67d172853491" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackernoon/6-ios-applications-tips-for-a-better-user-experience-67d172853491">6 iOS Applications Tips For a Better User Experience</a> was originally published in <a href="https://medium.com/hackernoon">HackerNoon.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Swift: Avoiding Memory Leaks by Examples]]></title>
            <link>https://medium.com/hackernoon/swift-avoiding-memory-leaks-by-examples-f901883d96e5?source=rss-4a8462234f26------2</link>
            <guid isPermaLink="false">https://medium.com/p/f901883d96e5</guid>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[cars]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[memory-management]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Jaafar Barek]]></dc:creator>
            <pubDate>Wed, 23 Jan 2019 08:12:37 GMT</pubDate>
            <atom:updated>2019-01-25T22:09:06.174Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vy54VZkKuf4zbd7WjdS75Q.png" /><figcaption><a href="https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/MemoryMgmt/Art/memory_management_2x.png">https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/MemoryMgmt/Art/memory_management_2x.png</a></figcaption></figure><p>In Swift Automatic Reference Counting (ARC) is used to manage memory usage in an iOS application.<br>Each time you create a new instance of a class ARC allocates a chunk of memory to store information about it, and automatically frees this memory when that instance is no longer in need.<br>You as a developer don’t need to do anything for that management , except for 3 cases were you need to tell ARC more information about relations between instances to avoid “Retain Cycles”.</p><p>In this article we will walk together in the process of managing these 3 cases , and see real examples of retain cycles and how to get rid of them. <br>But first, what are retain cycles and why we need to avoid them?</p><h3>Retain Cycles:</h3><p>Retain Cycle is the case when 2 objects have strong reference to each others and are retained , make it impossible for ARC to release those object from the memory and cause what we call a “memory leak”.</p><blockquote>Memory leaks are dangerous in you app because they will affect your app’s performance and might lead to crashes when the app runs out of memory.</blockquote><h3>The 3 cases that cause memory leaks:</h3><h4><strong>1- Strong Reference Between 2 Classes:</strong></h4><p>Suppose we have 2 classes (Author&amp;Book) that directly reference each other :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9f82ddf58172b3957ad9512234dfb4f2/href">https://medium.com/media/9f82ddf58172b3957ad9512234dfb4f2/href</a></iframe><p>Theoretically this should print that both objects were allocated , and then de allocated because they were set to nil but instead it prints the following:</p><pre><strong>Author Object was allocated in memory<br>Book object was allocated in memory</strong></pre><p>As you see both objects were never released from memory because a retain cycle occurred when both classed had strong reference to each others.</p><p>To solve this we can declare on of the references to be weak or unowned as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2cca8b4a679634990500f2b8e1344367/href">https://medium.com/media/2cca8b4a679634990500f2b8e1344367/href</a></iframe><p>This time both objects will be released and the console prints the following:</p><pre><strong>Author Object was allocated in memory<br>Book object was allocated in memory<br>Author Object was deallocated<br>Book Object was deallocated</strong></pre><p>The problem was solved and ARC was able to clean the memory chunk when the objects were released just by making one of the references weak , but what does weak and unowned actually mean? According to apple’s documentation:</p><h4>Weak References</h4><blockquote>A weak reference is a reference that does not keep a strong hold on the instance it refers to, and so does not stop ARC from disposing of the referenced instance. This behavior prevents the reference from becoming part of a strong reference cycle. You indicate a weak reference by placing the weak keyword before a property or variable declaration.</blockquote><h4>Unowned References</h4><blockquote>Like a weak reference, an <em>unowned reference</em> does not keep a strong hold on the instance it refers to. Unlike a weak reference, however, an unowned reference is used when the other instance has the same lifetime or a longer lifetime. You indicate an unowned reference by placing the unowned keyword before a property or variable declaration.</blockquote><h4><strong>2- Class Protocol Relation:</strong></h4><p>Another cause for memory leaks can be a strong relation between a protocol and a class. In the following example we will take a real world scenario were we have a TablViewController Class and a TableViewCell Class , when the user presses a button in the TableViewCell it should delegate this action to the TablViewController as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ecf8cada39aca488c04d664727388b00/href">https://medium.com/media/ecf8cada39aca488c04d664727388b00/href</a></iframe><p>Normally , when we dismiss the TableViewController , deinit should be called and the print statement should appear in the console , but in this case since TableViewCellDelegate and TableViewController have strong reference to each other they will never be released from memory.<br>To solve this we can simply adjust the TableViewCell class to be as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a56a91177d133193e016a4e951a71d52/href">https://medium.com/media/a56a91177d133193e016a4e951a71d52/href</a></iframe><p>This time dismiss the TableViewController and see the console:</p><pre><strong>TableViewController is deallocated</strong></pre><h4>3- Strong Reference Cycles for Closures:</h4><p>Suppose we have the following ViewController:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/25c45faa2bcfab5d937ff841d9fe4944/href">https://medium.com/media/25c45faa2bcfab5d937ff841d9fe4944/href</a></iframe><p>Try dismissing ViewController , the deinit method will never be executed.<br>The reason why is because the closure captures a strong reference of the ViewController. To solve this we need to pass self as weak or unowned as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/faf95261ab63014365eacdc7c5f89534/href">https://medium.com/media/faf95261ab63014365eacdc7c5f89534/href</a></iframe><p>This time when dismissing ViewController the console will print:</p><pre><strong>ClosureViewController was deallocated</strong></pre><h3>Conclusion</h3><p>There is no doubt ARC do an amazing job managing memory for our application , all we have to do as developers is to be aware of strong references between classes , between a class and a protocol , and inside closures by declaring weak or unowned variables in those cases.</p><h3>Some Great References about ARC:</h3><ul><li><a href="https://docs.swift.org/swift-book/LanguageGuide/AutomaticReferenceCounting.html">Apple’s Documentation.</a></li><li><a href="https://www.raywenderlich.com/959-arc-and-memory-management-in-swift">Raywenderlich</a> article about ARC.</li></ul><h3>Read my previous article about GCD in Swift:</h3><p><a href="https://medium.com/@BarekJaafar/swift-multi-threading-using-gcd-for-beginners-2581b7aa21cb">Swift Multi-Threading using GCD For Beginners.</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f901883d96e5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackernoon/swift-avoiding-memory-leaks-by-examples-f901883d96e5">Swift: Avoiding Memory Leaks by Examples</a> was originally published in <a href="https://medium.com/hackernoon">HackerNoon.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Swift Multi-Threading using GCD For Beginners.]]></title>
            <link>https://medium.com/hackernoon/swift-multi-threading-using-gcd-for-beginners-2581b7aa21cb?source=rss-4a8462234f26------2</link>
            <guid isPermaLink="false">https://medium.com/p/2581b7aa21cb</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[threading]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swift-4]]></category>
            <category><![CDATA[xcode]]></category>
            <dc:creator><![CDATA[Jaafar Barek]]></dc:creator>
            <pubDate>Sat, 19 Jan 2019 23:13:37 GMT</pubDate>
            <atom:updated>2019-03-24T12:13:07.779Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dJjow20BfCPn5bh8211khw.png" /></figure><h3><strong>Introduction: What is Grand Central Dispatch (GCD)</strong></h3><blockquote>According to Apple:<br>“GCD provides and manages FIFO queues to which your application can submit tasks in the form of block objects. Work submitted to dispatch queues are executed on a pool of threads fully managed by the system. No guarantee is made as to the thread on which a task executes.”</blockquote><p>In other words, GCD is an API provided by apple to allow you to manage concurrent operations in a smooth way, in order to avoid freezing of your application and keep it always responsive for users.</p><h3>Types of Queues:</h3><p>When dealing with GCD, there are 3 types of queues:</p><h4><strong>1- The Main Queue:</strong></h4><p>This Queue has the highest priority of all , and it runs on the main thread.<br>All UI updates should be done on this thread or otherwise lagging and weird crashes will occur in your application.<br>You use it simply like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/693/1*jC62XxHntUd7Gb7IEKU5gg.png" /></figure><h4>2- The Global Queue:</h4><p>This Queue is separated into 4 main types and a default type according to QOS(Quality of Service), from highest priority to lowest:</p><p>1- userInteractive : <em>Work is virtually instantaneous. Similar to main thread.<br></em>2- userInitiated : Work <strong>is</strong> nearly instantaneous, such <strong>as</strong> a few seconds or less.<br><strong>3- default</strong>: don’t use it usually , the type will be inferred by the system<br>4- utility: Work takes a few seconds to a few minutes.<br>5- background: Work takes significant time, such <strong>as</strong> minutes or hours.</p><p>Both, utility and background threads should be used for heavy operations that needs time in order not to block the main thread.<br>You can simply call one as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/692/1*_y6xTVvJQ0cdIqs9P0I8Vg.png" /></figure><h4>3- Custom Queues:</h4><p>Custom queues are queues that you can create on your own and give whatever QOS and attributes you want:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HHyZRAiUfRF2Kw51t0TngQ.png" /></figure><h3>Using Queues Together:</h3><p>Usually in a real world scenario you often need to use those queues together.<br>Let us say you need to do a heavy operation like downloading and image and then display this image in an imageView. <br>You do it as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/810/1*QPWzUUUEzhKMCyyB4rnl_A.png" /></figure><p>As you can notice we do the heavy operation that can take a lot of time inside a background thread and then display the image inside an imageView using the main thread.</p><h3>Async VS Sync:</h3><p>As you saw in the example above we used the word async after each queue.<br>With GCD, you can dispatch a task either synchronously or asynchronously.<br>Sync means that no other task will start until the sync operation finishes , while Async will allow other tasks to start even if it didn’t finish yet.</p><p>Let us take a look at real world examples:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/707/1*Coy9pJvN-gg6VsPy-OHBew.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/220/1*P7FaXqAI-RZelJeChvPHAg.png" /><figcaption>The Emojis are used to help distinguish between each thread.</figcaption></figure><p>Notice in the above example both tasks are async , they were executed at the same time. However the userInteractive thread had more priority of execution than the background thread which is normal as discussed before.<br>Now let us take a look if the first task was sync what will happen:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/682/1*EIzRlmpLJBeDsmtfIN1DPA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/187/1*L52NppUKB6b9VWYI4YNBbA.png" /></figure><p>As you can see although the userInteractive thread has higher priority it wasn’t executed at all until the first sync task was completed.</p><p>Even though the above example is simple, it clearly shows how a program behaves with queues running synchronously and asynchronously , so you should be aware of using sync unless needed , and use async in most of the cases to avoid blocking your application in front of your user.</p><h3>Conclusion:</h3><p>There are a lot more to know about managing threads in your application to be fit in a single article . But what is important as a beginner to know that not also you should perform slow or expensive processes on asynchronously , you should always perform them on low priority threads ( example: background thread), and always keep in mind to do any UI updates on the main thread.</p><h3>Some Great References about GCD:</h3><ul><li><a href="https://www.raywenderlich.com/5370-grand-central-dispatch-tutorial-for-swift-4-part-1-2">Ray Wenderlich</a> Tutorials on Threading.</li><li><a href="https://www.appcoda.com/grand-central-dispatch/">Appcoda</a> article about concurrency.</li><li>And of course .. <a href="https://developer.apple.com/documentation/dispatch">Apple Documentation</a>.</li></ul><h4><strong>If you are interested , read my article about Memory management in swift:</strong></h4><p><a href="https://medium.com/@BarekJaafar/swift-avoiding-memory-leaks-by-examples-f901883d96e5">Swift: Avoiding Memory Leaks by Examples</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2581b7aa21cb" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackernoon/swift-multi-threading-using-gcd-for-beginners-2581b7aa21cb">Swift Multi-Threading using GCD For Beginners.</a> was originally published in <a href="https://medium.com/hackernoon">HackerNoon.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>