<?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 Betty Kintzlinger on Medium]]></title>
        <description><![CDATA[Stories by Betty Kintzlinger on Medium]]></description>
        <link>https://medium.com/@bettykin?source=rss-bcefc73f232b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Hd4pF-QoHakPKB77.</url>
            <title>Stories by Betty Kintzlinger on Medium</title>
            <link>https://medium.com/@bettykin?source=rss-bcefc73f232b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 06:50:25 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bettykin/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to use GCD and survive to brag about it]]></title>
            <link>https://medium.com/@bettykin/how-to-use-gcd-and-survive-to-brag-about-it-fd7594249698?source=rss-bcefc73f232b------2</link>
            <guid isPermaLink="false">https://medium.com/p/fd7594249698</guid>
            <category><![CDATA[swift-3]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[grand-central-dispatch]]></category>
            <dc:creator><![CDATA[Betty Kintzlinger]]></dc:creator>
            <pubDate>Tue, 26 Sep 2017 19:59:55 GMT</pubDate>
            <atom:updated>2017-09-26T19:59:55.275Z</atom:updated>
            <content:encoded><![CDATA[<p>Grand Central Dispatch is a library that handles concurrent execution on iOS and OS X. It simplifies multi-thread work, and prioritizes tasks to optimize the performance of your app.</p><p>It looks scary at first, but getting to know GCD will make you want to experience it yourself (and then brag about it, obviously).</p><h4><strong>When to use GCD</strong></h4><ul><li>When you want to avoid blocking user interaction, such as logic that takes time, network requests, parsing data or anything that can cause your app to be unresponsive.</li><li>When you want to split heavy work between several threads to speed up your app.</li><li>When you’re using a data structure that is being accessed from different threads. Using GCD would help you avoid using corrupted data, caused by several threads reading and writing to it at the same time.</li></ul><h4><strong>Get to know GCD</strong></h4><p>GCD uses queues to perform tasks, therefore tasks in a dispatch queue are being handled in FIFO order. This means that tasks start in the order that they were added to the queue.</p><p>All dispatch queues that GCD provides are thread-safe, and therefore can be accessed from multiple threads without any worries.</p><p>GCD has 2 kinds of queues:</p><p><strong>Serial queues </strong>- only one task is executed each time. We already know that tasks are performed in the order that they were added to the queue, this guarantees that code won’t be accessed concurrently from multiple tasks in the queue. (Sounds like a way to handle reader-writer problem)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/614/1*hmZzx-YiNUSL4XLmb-HeBA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*DS2zYnSDrxKrVXzYN_0OVQ.xml" /></figure><p><strong>Concurrent queues </strong>-<strong> </strong>multiple tasks can execute concurrently. You only know that they start in the order they were added, but you don’t know in which order they finish or how many tasks are executing at a given time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/456/1*GD1k0l1FsF55ItfM0G60pQ.png" /></figure><p>Using GCD properly means choosing the right queue and function for the job, so lets take a look at the queues that GCD has to offer.</p><h4><strong>GCD queues</strong></h4><p>There are 5 dispatch queues used by the system. You can access those queues but keep in mind not to abuse them, your’e not the only one using them.</p><ul><li>One serial queue, which is the <strong>main queue</strong>.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/162e61cbf7451174ad9082a784549972/href">https://medium.com/media/162e61cbf7451174ad9082a784549972/href</a></iframe><p>This queue is the only queue which is allowed to update the UI. If this queue gets blocked it also means that the UI is blocked, that the app is not responsive and that nothing gets updated. So don’t call the main queue synchronously from the main queue. It will cause the main queue to wait for itself forever, as shown in the code below that you should never write:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/793f63a9ba899e82e2d0e9a57524b9f0/href">https://medium.com/media/793f63a9ba899e82e2d0e9a57524b9f0/href</a></iframe><p>Take into consideration that any serial queue calling itself synchronously will block the queue.</p><ul><li>4 concurrent queues - otherwise called <strong>global queues.</strong> The differentiation Between each queue is its priority, which could either be background, low, default or high.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/807b3ae8710edf82c62929cea77e8098/href">https://medium.com/media/807b3ae8710edf82c62929cea77e8098/href</a></iframe><p>How do you decide which priority to use? and what is qos?</p><p><strong>QoS</strong> stands for Quality of Service. OS decides which code should run at any given time, but it also allows you to participate in that decision making. This is how: QoS is an Enum you can use to tell OS which code is more urgent. QoS cases are also called classes and they are:</p><ul><li><strong>User Interactive:</strong> Highest priority. Use it for UI related tasks or small tasks that should run immediately. In other words, OS will run it on the main thread.</li><li><strong>User Initiated:</strong> High priority. Use it for tasks that are triggered by the user and should run right away and finish fast but shouldn’t run on the main thread.</li><li><strong>Default: </strong>This is the OS default option when there isn’t enough information to determine other quality of service (as in the first example).</li><li><strong>Utility: </strong>Low priority. Use it for tasks that require other system resources or longer tasks such as Network communication. Tasks here can take a few minutes to finish.</li><li><strong>Background: </strong>Background priority (you guessed it, didn’t you?). Use it for tasks that take a lot of time to finish. Tasks that the user is not aware of such as maintenance tasks.</li></ul><p>In addition to the main queue and the global queues, GCD provides you the option to create your own custom queues, either serial queues or concurrent queues.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e650ba379c686b2200fcf952b10179fb/href">https://medium.com/media/e650ba379c686b2200fcf952b10179fb/href</a></iframe><p>Only the first argument in this init method is necessary. If you don’t specify anything for one of the other arguments it will fall back to default. Let’s look at some interesting arguments of this method:</p><p><strong>label</strong>: String. A unique label for your dispatch queue, reverse DNS is a common choice here.</p><p><strong>qos</strong>: DispatchQoS. You already know what it means :). Think of the priority for this dispatch queue your’e creating. When this argument is not specified the priority will be default.</p><p><strong>attributes</strong>: DispatchQueue.Attributes. Enum with 2 cases, <em>concurrent </em>which makes this queue a concurrent queue, and <em>initiallyInactive </em>which tells the system not to run this queue yet, you will need to start the execution yourself by running queue.activate(). By the way, to create an initiallyInactive concurrent queue you simply give this argument an array with both values: [.concurrent, .initiallyInactive]. When this argument is not specified the queue will be serial and activated.</p><p>Let’s do one more thing with dispatch queue, we will handle the read-write problem.</p><p>So you have an array with data. What happens when multiple threads read and write to it simultaneously? It’s not safe to let one thread read while another thread is modifying the data.</p><p>Here’s how our simple data component looks:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/adfb947baf90d2eb24c7a61bd355a0d5/href">https://medium.com/media/adfb947baf90d2eb24c7a61bd355a0d5/href</a></iframe><p>You can see that the read method doesn’t check if dataArray is not being modified when retrieving the array. Also the write method doesn’t make sure the array is not being read while it changes the array.</p><p>One solution is to use a serial queue:</p><ul><li>Create a serial queue.</li><li>read(): Put the read method inside sync() method. Our read method must return a value so we can’t use async() method since it returns without waiting for the task to complete.</li><li>write(): Here we can use async() method, no reason to wait, the task will be executed in its turn.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/673e12c7e222bde981abb9a566fd203a/href">https://medium.com/media/673e12c7e222bde981abb9a566fd203a/href</a></iframe><p>This way you make sure that dataArray cannot be read and write to at the same time.</p><p>Another solution can use concurrent queue efficiency, by letting all tasks run concurrently but uses a barrier on the block of code of writing to dataArray. This way when a task of writing is inserted to the queue it will wait for all other tasks to finish before it runs. Tasks inserted after this one will wait for it to finish before they start, so when writing to dataArray no reading is executing.</p><p>To do this we will use DispatchWorkItemFlags argument with enumeration case <em>.barrier</em> when calling async() method.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d158683d8089cad1777414d32b959571/href">https://medium.com/media/d158683d8089cad1777414d32b959571/href</a></iframe><h4>Summary</h4><p>This post introduces GCD and how it works. It shows how to get access to the main queue, the global queues, how to create your own queues (both serial and concurrent) and finally demonstrates a lock in GCD style. There are more functions and concepts in GCD not mentioned here. I hope I got you curious about GCD and you will keep exploring it.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fd7594249698" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>