<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Tarik Dahic</title>
    <description>The latest articles on DEV Community by Tarik Dahic (@daholino).</description>
    <link>https://dev.to/daholino</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F225609%2F4f8bf860-ee79-432c-9d1c-4ea33771350a.png</url>
      <title>DEV Community: Tarik Dahic</title>
      <link>https://dev.to/daholino</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/daholino"/>
    <language>en</language>
    <item>
      <title>macOS storage cleaning tips for iOS developers</title>
      <dc:creator>Tarik Dahic</dc:creator>
      <pubDate>Fri, 02 Feb 2024 21:53:21 +0000</pubDate>
      <link>https://dev.to/daholino/macos-storage-cleaning-tips-for-ios-developers-409d</link>
      <guid>https://dev.to/daholino/macos-storage-cleaning-tips-for-ios-developers-409d</guid>
      <description>&lt;p&gt;While we work as developers we are constantly downloading, installing, moving and changing files on our drive. This results to populating the drive to its limit, at least in my case. In this article I will show you two tools that I use on my Mac to detect and free the used storage.&lt;/p&gt;

&lt;h2&gt;
  
  
  DevCleaner
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;DevCleaner&lt;/strong&gt; is a great macOS app for freeing the storage used by Xcode. It is developed by &lt;a href="https://twitter.com/vashpan"&gt;Konrad Kołakowski&lt;/a&gt; and it is available on &lt;a href="https://apps.apple.com/us/app/devcleaner-for-xcode/id1388020431?mt=12"&gt;Mac App Store&lt;/a&gt; and &lt;a href="https://github.com/vashpan/xcode-dev-cleaner"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The app allows you to clean device support files from development phones, build archives, derived data, documentation cache and old simulator &amp;amp; device logs. I found it really useful to quickly free the data that I don’t need.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gVe4O3Y7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://tarikdahic.com/images/devcleaner.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gVe4O3Y7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://tarikdahic.com/images/devcleaner.png" alt="DevCleaner window" width="769" height="826"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  GrandPerspective
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;GrandPerspective&lt;/strong&gt; is a compact macOS utility that visually represents disk usage in a file system. It aids in disk management by allowing you to quickly identify the files and folders consuming the most space. The visualization employs a tree map, where each file is depicted as a rectangle, its size proportional to the actual file size. Files within the same folder are grouped together, but their placement is otherwise random.&lt;/p&gt;

&lt;p&gt;It is developed by &lt;a href="https://bonsma.home.xs4all.nl/"&gt;Erwin Bonsma&lt;/a&gt;. More info can be found on its &lt;a href="https://grandperspectiv.sourceforge.net/"&gt;SourceForge&lt;/a&gt; page.&lt;/p&gt;

&lt;p&gt;This app helped me a lot of times to identify what is taking up my drive space. It is very simple to use. When opening the app you pick the base folder from where the scanning will start and after the scanning is done you will get a view with rectangular blobs showing what takes your disk space. I often choose the root (/) folder for the scanning so that I can see all the files on my computer.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Q6P8Yt49--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://tarikdahic.com/images/grandperspective.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Q6P8Yt49--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://tarikdahic.com/images/grandperspective.png" alt="GrandPerspective window" width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you have any other tips or apps for freeing storage please leave them in the comments. I hope that this article helped you to understand what takes up the space on your macOS computer.&lt;/p&gt;

</description>
      <category>macos</category>
      <category>storage</category>
    </item>
    <item>
      <title>Limit concurrent function executions using Go channels</title>
      <dc:creator>Tarik Dahic</dc:creator>
      <pubDate>Mon, 21 Aug 2023 17:49:11 +0000</pubDate>
      <link>https://dev.to/daholino/limit-concurrent-function-executions-using-go-channels-ceb</link>
      <guid>https://dev.to/daholino/limit-concurrent-function-executions-using-go-channels-ceb</guid>
      <description>&lt;p&gt;This article will explain how to limit concurrent function execution using Go channels. I worked on a Go service that executes another program inside its HTTP request handler. The program that is executed uses a lot of CPU usage and memory usage since it works with photos and videos so I had to limit how many of them could be running at the same time.&lt;/p&gt;

&lt;p&gt;Luckily, Go channels come very handy in situations like these. We will also add timeout support so that our waiting goroutines don’t block indefinitely. Buffered Go channels will be needed to develop this so let’s remember what they are.&lt;/p&gt;

&lt;h2&gt;
  
  
  Buffered Go channels
&lt;/h2&gt;

&lt;p&gt;Buffered channels can hold a limited number of values (determined by the buffer size), and will only block the sending goroutine when the buffer is full. This can allow for some additional concurrency, but requires careful consideration to avoid deadlocks and other synchronization issues.&lt;/p&gt;

&lt;p&gt;Buffered channels are an ideal solution for what we are going to build in this article.&lt;/p&gt;

&lt;h2&gt;
  
  
  Limiting concurrent function execution
&lt;/h2&gt;

&lt;p&gt;We start by creating a new package called &lt;code&gt;limiter&lt;/code&gt; and we define a data structure for &lt;code&gt;Limiter&lt;/code&gt; type. We will provide a limit and a timeout when creating a &lt;code&gt;Limiter&lt;/code&gt; and the data structure will hold that timeout and a buffered channel created with provided limit.&lt;/p&gt;

&lt;p&gt;Translated to code we can write something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Limiter&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;timeout&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Duration&lt;/span&gt;
    &lt;span class="n"&gt;ch&lt;/span&gt; &lt;span class="k"&gt;chan&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt;&lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;NewLimiter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;limit&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Duration&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Limiter&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Limiter&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;ch&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;make&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;chan&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt;&lt;span class="p"&gt;{},&lt;/span&gt; &lt;span class="n"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We now need to define how a &lt;code&gt;Limiter&lt;/code&gt; type will be used. I thought that it would be the best if we could pass a function that takes no arguments and returns no values to the limiter. If the buffer of a buffered channel is not full then the passed function will be executed. If the buffer is full then the caller will wait for a timeout that was defined when &lt;code&gt;Limiter&lt;/code&gt; was created. If a slot is freed then the function will be executed and if time for timeout passes an error will be returned.&lt;/p&gt;

&lt;p&gt;The buffered channel is created with the type of an empty struct. Empty structs are useful for signaling where data is not important because their memory footprint is 0. You can find more on empty structs reading &lt;a href="https://dave.cheney.net/2014/03/25/the-empty-struct"&gt;this&lt;/a&gt; great article.&lt;/p&gt;

&lt;p&gt;We can see the definition of &lt;code&gt;Execute&lt;/code&gt; function in the code below.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Limiter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;Execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="kt"&gt;error&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ch&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt;&lt;span class="p"&gt;{}{}&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
        &lt;span class="c"&gt;// Added execution for running...&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;After&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;errors&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;New&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"execution timed out"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c"&gt;// On exit, remove this execution from limit channel&lt;/span&gt;
    &lt;span class="k"&gt;defer&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ch&lt;/span&gt;
    &lt;span class="p"&gt;}()&lt;/span&gt;

    &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;code&gt;Execute&lt;/code&gt; function first tries to insert the empty struct into the buffered channel. If the channel is not full, the select statement will continue and the provided function will be executed. But, if the channel is full the select statement will wait and two things can happen. The buffered channel will be emptied below its capacity and we will execute the provided function. If the amount of time passes as defined in the timeout value of the &lt;code&gt;limiter&lt;/code&gt; the select statement will enter the case where it returns the error. In that case, function will not be executed. Before invoking the provided function we add a function with defer statement that will be executed when &lt;code&gt;Execute&lt;/code&gt; completes. This function will that the buffered channel is being emptied.&lt;/p&gt;

&lt;p&gt;This is all for &lt;code&gt;limiter&lt;/code&gt;, a struct that can limit function calls. I would also like to point that in a select statement you can define custom time intervals using channels like in the example code above. This can be very useful to check some things periodically or to do timeouts like in this example.&lt;/p&gt;

&lt;p&gt;Here is an example on how to use the structure that we’ve defined:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="c"&gt;// 1. Define the limiter&lt;/span&gt;
&lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;limiter&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewLimiter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;20&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Second&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c"&gt;// 2. Somewhere in your code you can add a function for execution   &lt;/span&gt;
&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c"&gt;// Processing..&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;If you want to see the entire file I’ve created a gist on GitHub so you can visit &lt;a href="https://gist.github.com/daholino/ed3290aa40834979e9d6bc065bdbc76b"&gt;this&lt;/a&gt; link or copy the code from the GitHub embed below.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Thank you for reading this article and I hope that it helped you to solve your problem or learn something new.&lt;/p&gt;

</description>
      <category>go</category>
      <category>channels</category>
    </item>
    <item>
      <title>Non-blocking sequential processing in Go using infinite (unbounded) buffered channel</title>
      <dc:creator>Tarik Dahic</dc:creator>
      <pubDate>Wed, 09 Aug 2023 18:04:31 +0000</pubDate>
      <link>https://dev.to/daholino/non-blocking-sequential-processing-in-go-using-infinite-unbounded-buffered-channel-360g</link>
      <guid>https://dev.to/daholino/non-blocking-sequential-processing-in-go-using-infinite-unbounded-buffered-channel-360g</guid>
      <description>&lt;p&gt;While developing the backend service that will handle a lot of real-time events at the company I work for we had a requirement to queue and process events sequentially while not blocking the reader that is reading them. Representing this requirement in a diagram looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi214fib4f988rhsxfrah.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi214fib4f988rhsxfrah.png" alt="Processing requirement"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After the event is queued for processing the control is given to the reader so that it could read new events and not block the reading.&lt;/p&gt;

&lt;p&gt;In Go this type of problem cannot be solved without using channels and goroutines so we will go down that road.&lt;/p&gt;

&lt;h2&gt;
  
  
  Buffered and unbuffered Go channels
&lt;/h2&gt;

&lt;p&gt;When creating a Go channel we can create it as a buffered or an unbuffered channel. Here is an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;ch&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;make&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;chan&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// Unbuffered&lt;/span&gt;
&lt;span class="n"&gt;ch&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;make&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;chan&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// Buffered&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Unbuffered channels block the sending goroutine until there is a corresponding receiver ready to receive the value being sent. This means that data is guaranteed to be received in the order it was sent, and that synchronization is built into the channel. However, if we send one event as per our requirement and it starts processing, the next event will block and will have to wait for the first one to complete processing. The reading of the next event will be blocked. We don’t want that.&lt;/p&gt;

&lt;p&gt;Buffered channels, on the other hand, can hold a limited number of values (determined by the buffer size), and will only block the sending goroutine when the buffer is full. This can allow for some additional concurrency, but requires careful consideration to avoid deadlocks and other synchronization issues. On the first thought, this could work, if we know the system demand we can provide a buffer big enough so that we don’t worry that it will get blocked.&lt;/p&gt;

&lt;p&gt;But, we know better and we want to eliminate all the ifs. That is why we will build a Go buffered channel with infinite (unbounded) capacity.&lt;/p&gt;
&lt;h2&gt;
  
  
  Go buffered channel with infinite capacity
&lt;/h2&gt;

&lt;p&gt;Let’s start with a name. I named my package and the data structure &lt;code&gt;executor&lt;/code&gt;. There are a lot of use cases for queuing data so we will need to use Go generics so that every type of data can be passed into the &lt;code&gt;executor&lt;/code&gt;. Go generics are available from Go version 1.18 and up. We also need to define the &lt;code&gt;executor&lt;/code&gt; API, how it will be used when it is used in Go software. I decided to pass the function to the &lt;code&gt;executor&lt;/code&gt; that will be called within when some data is ready to be processed.&lt;/p&gt;

&lt;p&gt;To make it easy for ourselves we can define a new type that will describe this function:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;ExecHandler&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="n"&gt;any&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Since we will be queuing something, preferably, on another goroutine, we will need a pair of channels to send data from that goroutine and to read data for processing. A data structure encapsulating all of the mentioned above could be defined like:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Executor&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="n"&gt;any&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;reader&lt;/span&gt; &lt;span class="k"&gt;chan&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;
    &lt;span class="n"&gt;writer&lt;/span&gt; &lt;span class="k"&gt;chan&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;
    &lt;span class="n"&gt;buffer&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;
    &lt;span class="n"&gt;execHandler&lt;/span&gt; &lt;span class="n"&gt;ExecHandler&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Buffer property will hold queued elements that are waiting to be processed.&lt;/p&gt;

&lt;p&gt;We also need to provide a factory function for &lt;code&gt;executor&lt;/code&gt; so that everything is properly initialised:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;New&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="n"&gt;any&lt;/span&gt;&lt;span class="p"&gt;](&lt;/span&gt;&lt;span class="n"&gt;execHandler&lt;/span&gt; &lt;span class="n"&gt;ExecHandler&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Executor&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Executor&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;]{&lt;/span&gt;
        &lt;span class="n"&gt;reader&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;make&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;chan&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="n"&gt;writer&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;make&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;chan&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;make&lt;/span&gt;&lt;span class="p"&gt;([]&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="n"&gt;execHandler&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;execHandler&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;go&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;code&gt;New&lt;/code&gt; function constructs and returns the &lt;code&gt;executor&lt;/code&gt; object. It also spawns a new goroutine and calls a &lt;code&gt;run&lt;/code&gt; method on &lt;code&gt;executor&lt;/code&gt; which starts listening for new elements that will be queued for processing.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Executor&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;go&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;listenForReading&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;reader&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
            &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;writer&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;writer&lt;/span&gt;
            &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Executor&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="n"&gt;listenForReading&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;reader&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;execHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;There’s a lot happening in the &lt;code&gt;run&lt;/code&gt; method and we will now break it down. In the first line we spawn a new goroutine that will be doing processing of queued elements. By processing I mean that the exec handler will be called with that element.&lt;/p&gt;

&lt;p&gt;Next, we have an infinite loop that queues and sends data to processing. If the buffer of queued elements is empty we wait for a new element from &lt;code&gt;writer&lt;/code&gt; channel. When we get a new element it is stored in the &lt;code&gt;buffer&lt;/code&gt; and the new iteration of the loop begins.&lt;/p&gt;

&lt;p&gt;If the buffer is not empty, we stop on a select statement where two things can happen:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;reader&lt;/code&gt; channel is ready, element can be dispatched to processing and it will be removed from the &lt;code&gt;buffer&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;In case a new element is waiting to be queued it will be added to the buffer.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Only thing left is to add an exported method to &lt;code&gt;executor&lt;/code&gt; for queuing elements:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Executor&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="n"&gt;Dispatch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;writer&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;That would be it, we now have a functioning infinite (unbounded) channel that processes data sequentially.&lt;/p&gt;

&lt;p&gt;If you want to see the entire file I’ve created a gist on GitHub so you can visit &lt;a href="https://gist.github.com/daholino/f3c4e2ddaf15e03aa4a2a22d1228a8c5" rel="noopener noreferrer"&gt;this&lt;/a&gt; link or copy the code from the GitHub embed below.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;There are a lot of solutions to this problem so if you have one that differs from the one in the article please discuss it below. I would love to see what else is possible. If you are working in other technologies or in Go, I encourage you to play with stuff like this to get better understanding of the language and its concurrency mechanisms.&lt;/p&gt;

</description>
      <category>go</category>
      <category>channels</category>
      <category>goroutine</category>
    </item>
    <item>
      <title>Calculate values in background and use the result after with Swift concurrency</title>
      <dc:creator>Tarik Dahic</dc:creator>
      <pubDate>Thu, 13 Oct 2022 20:07:08 +0000</pubDate>
      <link>https://dev.to/daholino/calculate-values-in-background-and-use-the-result-after-with-swift-concurrency-2mbe</link>
      <guid>https://dev.to/daholino/calculate-values-in-background-and-use-the-result-after-with-swift-concurrency-2mbe</guid>
      <description>&lt;p&gt;If you need to compute a value and don’t want to block the main thread you can do so by calculating that value in a Swift Task structure that will return that computed value. Prior to Swift concurrency you would probably jump to a background queue and compute the value that you need and proceed with some action if necessary.&lt;/p&gt;

&lt;p&gt;Let’s jump into an example. I defined a struct called &lt;code&gt;Example&lt;/code&gt; that will create a Task at its init in which it will try to compute and return a &lt;code&gt;String&lt;/code&gt; value. The struct will also have an async method called &lt;code&gt;value()&lt;/code&gt; which will return that computed value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;struct&lt;/span&gt; &lt;span class="kt"&gt;Example&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;someTask&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Task&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;Error&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Task&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Some calculation&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="kt"&gt;Task&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;nanoseconds&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3_000_000_000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"example"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;value&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;throws&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;someTask&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we want to access the computed value we can use the &lt;code&gt;value&lt;/code&gt; property that is defined on the Swift Task structure. If the task hasn’t completed, accessing this property waits for it to complete and its priority increases to that of the current task.&lt;/p&gt;

&lt;p&gt;To use the &lt;code&gt;Example&lt;/code&gt; structure we need to instantiate it and read the value from an async context:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;example&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Example&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="kt"&gt;Task&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Value is:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;example&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;value&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the Task will not throw an error then its type would be &lt;code&gt;Task&amp;lt;ReturnType, Never&amp;gt;&lt;/code&gt; and we wouldn’t need to use &lt;code&gt;try&lt;/code&gt; keyword when accessing the task’s value.&lt;/p&gt;

&lt;p&gt;This has helped me in some situation where I want to calculate something in advance and if user requests it that I instantly have it. For example, I wanted to compress the image from camera in case user presses the button to send the image. I start the compression on the image preview screen and then when user wants to send it I have the compressed image ready.&lt;/p&gt;

</description>
      <category>ios</category>
      <category>swift</category>
      <category>structuredconcurrency</category>
    </item>
    <item>
      <title>Wrapping delegates with Swift async/await and continuations</title>
      <dc:creator>Tarik Dahic</dc:creator>
      <pubDate>Thu, 30 Jun 2022 13:52:44 +0000</pubDate>
      <link>https://dev.to/daholino/wrapping-delegates-with-swift-asyncawait-and-continuations-4l4p</link>
      <guid>https://dev.to/daholino/wrapping-delegates-with-swift-asyncawait-and-continuations-4l4p</guid>
      <description>&lt;p&gt;In this article you will learn how to convert or use existing delegate patterns and wrap them with Swift’s structured concurrency to use it with async/await mechanism in your apps.&lt;/p&gt;

&lt;p&gt;I will show you an example with &lt;code&gt;AVCapturePhotoCaptureDelegate&lt;/code&gt; to start capturing a photo and return the resulting &lt;code&gt;UIImage&lt;/code&gt; when the capture is over.&lt;/p&gt;

&lt;h3&gt;
  
  
  Adopting delegate and using completion handler
&lt;/h3&gt;

&lt;p&gt;In the code example below I implemented a simple class &lt;code&gt;CameraPhotoProcessor&lt;/code&gt; and that class adopts the &lt;code&gt;AVCapturePhotoCaptureDelegate&lt;/code&gt; protocol. Its responsibility is to start capturing the photo and convert it to a &lt;code&gt;UIImage&lt;/code&gt; when the photo is captured. It is done asynchronously. I introduced a completion handler that can notify the code that calls the &lt;code&gt;startCapture()&lt;/code&gt; when the photo is actually captured. The completion handler will be invoked with Swift’s &lt;code&gt;Result&lt;/code&gt; type and it will be one of the two supported values, success with image object or failure with error object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;CameraPhotoProcessor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;NSObject&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;completion&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="kt"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;UIImage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;Error&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Void&lt;/span&gt;&lt;span class="p"&gt;)?&lt;/span&gt;

    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;startCapture&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="nv"&gt;photoOutput&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;AVCapturePhotoOutput&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;using&lt;/span&gt; &lt;span class="nv"&gt;settings&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;AVCapturePhotoSettings&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;completion&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;@escaping&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;UIImage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;Error&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;completion&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;completion&lt;/span&gt;
        &lt;span class="n"&gt;photoOutput&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;capturePhoto&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;with&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;settings&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;delegate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// MARK: - AVCapturePhotoCaptureDelegate&lt;/span&gt;

&lt;span class="kd"&gt;extension&lt;/span&gt; &lt;span class="kt"&gt;CameraPhotoProcessor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;AVCapturePhotoCaptureDelegate&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;photoOutput&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;output&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;AVCapturePhotoOutput&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;didFinishProcessingPhoto&lt;/span&gt; &lt;span class="nv"&gt;photo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;AVCapturePhoto&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;?)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;error&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
             &lt;span class="nf"&gt;completion&lt;/span&gt;&lt;span class="p"&gt;?(&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;failure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
             &lt;span class="k"&gt;return&lt;/span&gt;
         &lt;span class="p"&gt;}&lt;/span&gt;

         &lt;span class="k"&gt;guard&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;photo&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fileDataRepresentation&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
             &lt;span class="nf"&gt;completion&lt;/span&gt;&lt;span class="p"&gt;?(&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;failure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;CustomError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Cannot get photo file data representation"&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
             &lt;span class="k"&gt;return&lt;/span&gt;
         &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;guard&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;image&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;UIImage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;completion&lt;/span&gt;&lt;span class="p"&gt;?(&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;failure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;CustomError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Invalid photo data"&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="nf"&gt;completion&lt;/span&gt;&lt;span class="p"&gt;?(&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;success&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;image&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can see that the code above is error prone. The completion handler that will be passed will need to use the &lt;code&gt;[weak self]&lt;/code&gt; capturing to avoid creating retain cycles. Let’s say that we have a function that is called when the capture button is pressed. This is how we would use the code above:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;captureButtonPressed&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;photoProcessor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;startCapture&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;from&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;photoOutput&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;using&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;settings&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;weak&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;
        &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;success&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;image&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
                &lt;span class="c1"&gt;// We have the image and now we can pass it or process it further&lt;/span&gt;
            &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;failure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;error&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
                &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;localizedDescription&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When using completion handlers we always need to watch out where the code will continue executing and what will we do beneath or inside the closure. For an experienced developer it doesn’t matter but when using the async/await the code looks a lot cleaner and less error prone.&lt;/p&gt;

&lt;h3&gt;
  
  
  Introducing continuations
&lt;/h3&gt;

&lt;p&gt;Continuations are mechanisms to interface between synchronous and asynchronous code. They wrap our existing code and wait for us to notify them about changes. The notifying part is called the &lt;code&gt;resume&lt;/code&gt; operation that we can use on the continuation.&lt;/p&gt;

&lt;p&gt;There are two types of continuations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Checked continuation - This type of continuation performs checks if continuation is resumed more than one time or if it is resumed after some time. Resuming from a continuation more than once is undefined behavior. Never resuming leaves the task in a suspended state indefinitely, and leaks any associated resources. Checked continuation logs a message if either of these invariants is violated.&lt;/li&gt;
&lt;li&gt;Unsafe continuation - Checked continuation performs runtime checks for missing or multiple resume operations. Unsafe continuation avoids enforcing these invariants at runtime because it aims to be a low-overhead mechanism for interfacing Swift tasks with event loops, delegate methods, callbacks, and other non-async scheduling mechanisms.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;During development, the ability to verify that the invariants are being upheld in testing is important. Because both types have the same interface, you can replace one with the other in most circumstances, without making other changes.&lt;/p&gt;

&lt;p&gt;Continuations can also be throwing or non-throwing. We can create them using global functions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;withCheckedContinuation()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;withCheckedThrowingContinuation()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;withUnsafeContinuation()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;withUnsafeThrowingContinuation()&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Applying continuations
&lt;/h3&gt;

&lt;p&gt;We will now use the theory from above and apply it onto our example with capturing a photo. Instead of creating a &lt;code&gt;completion&lt;/code&gt; property we will now have an &lt;code&gt;imageContinuation&lt;/code&gt; property.&lt;/p&gt;

&lt;p&gt;In this example I will use the checked throwing continuation because even though I know that my resume operation on continuation will be called one time. The throwing part is needed because our photo capturing operation can fail and we need a way to throw errors when the operation is aborted or failed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;CameraPhotoProcessor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;NSObject&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;imageContinuation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;CheckedContinuation&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;UIImage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;Error&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt;

    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;startCapture&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="nv"&gt;photoOutput&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;AVCapturePhotoOutput&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;using&lt;/span&gt; &lt;span class="nv"&gt;settings&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;AVCapturePhotoSettings&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;throws&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;UIImage&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;withCheckedThrowingContinuation&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;continuation&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;
            &lt;span class="n"&gt;imageContinuation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;continuation&lt;/span&gt;
            &lt;span class="n"&gt;photoOutput&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;capturePhoto&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;with&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;settings&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;delegate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// MARK: - AVCapturePhotoCaptureDelegate&lt;/span&gt;

&lt;span class="kd"&gt;extension&lt;/span&gt; &lt;span class="kt"&gt;CameraPhotoProcessor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;AVCapturePhotoCaptureDelegate&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;photoOutput&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;output&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;AVCapturePhotoOutput&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;didFinishProcessingPhoto&lt;/span&gt; &lt;span class="nv"&gt;photo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;AVCapturePhoto&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;?)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;error&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
             &lt;span class="n"&gt;imageContinuation&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resume&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;throwing&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
             &lt;span class="k"&gt;return&lt;/span&gt;
         &lt;span class="p"&gt;}&lt;/span&gt;

         &lt;span class="k"&gt;guard&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;photo&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fileDataRepresentation&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
             &lt;span class="n"&gt;imageContinuation&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resume&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;throwing&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;CustomError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Cannot get photo file data representation"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
             &lt;span class="k"&gt;return&lt;/span&gt;
         &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;guard&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;image&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;UIImage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;imageContinuation&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resume&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;throwing&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;CustomError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Invalid photo data"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;imageContinuation&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resume&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;returning&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;image&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And to use this in a Swift &lt;code&gt;Task&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;captureButtonPressed&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;Task&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;image&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;startCapture&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;from&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;photoOutput&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;using&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;settings&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="c1"&gt;// We have the image and now we can pass it or process it further&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;error&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;localizedDescription&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using Swift’s structured concurrency is more safe and less error prone as we can see. It also improves readability when multiple async operations are used. Imagine that we have three async operations using completion handlers. In that case we would need to indent the code three levels to use the final result. When using async/await we don’t have that. Everything is a straight line :)&lt;/p&gt;

</description>
      <category>swift</category>
      <category>ios</category>
    </item>
    <item>
      <title>Making properties read only outside their scope using private(set) in Swift</title>
      <dc:creator>Tarik Dahic</dc:creator>
      <pubDate>Fri, 22 Apr 2022 15:06:28 +0000</pubDate>
      <link>https://dev.to/daholino/making-properties-read-only-outside-their-scope-using-privateset-in-swift-3o6k</link>
      <guid>https://dev.to/daholino/making-properties-read-only-outside-their-scope-using-privateset-in-swift-3o6k</guid>
      <description>&lt;p&gt;I recently discovered Swift feature where setter for class and struct properties could be set to private while getter is internal or publicly accessed. This allows us to expose properties to the outside world without worrying if someone is going to change them in the future. I mostly do this to make dependencies of some type accessible.&lt;/p&gt;

&lt;p&gt;The example below shows how could we use this in a class or a struct.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;Example&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// prop will have an internal access modifier&lt;/span&gt;
  &lt;span class="kd"&gt;private(set)&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;prop&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;Type&lt;/span&gt;

  &lt;span class="c1"&gt;// prop with public access modifier&lt;/span&gt;
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;private(set)&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;prop&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;Type&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;prop&lt;/code&gt; can be changed (set) only in the scope of the encapsulated &lt;em&gt;struct&lt;/em&gt; or &lt;em&gt;class&lt;/em&gt; but the outside world can only read it.&lt;/p&gt;

&lt;p&gt;Setting a private setter only works with properties marked as &lt;code&gt;var&lt;/code&gt; and not &lt;code&gt;let&lt;/code&gt; because properties marked with &lt;code&gt;let&lt;/code&gt; are constants and by nature they are immutable.&lt;/p&gt;

&lt;p&gt;This Swift feature could also be used in cases when we have a read-only property backed by another private property. Something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;Example&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;_prop&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;Type&lt;/span&gt;

  &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;prop&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;Type&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;get&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;_prop&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Using private(set) makes code more readable and reduces the complexity:&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;Example&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;private(set)&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;prop&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;Type&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>swift</category>
      <category>ios</category>
    </item>
    <item>
      <title>Fetch and display the latest git commit hash in your iOS apps</title>
      <dc:creator>Tarik Dahic</dc:creator>
      <pubDate>Thu, 25 Nov 2021 21:17:54 +0000</pubDate>
      <link>https://dev.to/daholino/fetch-and-display-the-latest-git-commit-hash-in-your-ios-apps-1fp2</link>
      <guid>https://dev.to/daholino/fetch-and-display-the-latest-git-commit-hash-in-your-ios-apps-1fp2</guid>
      <description>&lt;p&gt;If you want to know from what exact commit your app is built you can include the latest git commit hash to the place where you display the app version. I like to call that generated hash &lt;strong&gt;“revision”&lt;/strong&gt; or in short &lt;strong&gt;“rev”&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Displaying this can be useful when you are distributing internal versions of your apps to QA engineers and you want to know on what app version they are testing. While the changes are coming in on development branches we don’t increase version or build numbers, we follow the revision value and always know what version is in use.&lt;/p&gt;

&lt;p&gt;In this article I will explain how to fetch the latest git commit hash in Xcode and display it in the iOS app.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fetching the latest git commit hash
&lt;/h2&gt;

&lt;p&gt;The procedure I will describe adds a new row to your &lt;code&gt;Info.plist&lt;/code&gt; file with specified key and sets the value to the short git commit hash.&lt;/p&gt;

&lt;p&gt;The first step is to go to your target’s &lt;strong&gt;Build phases&lt;/strong&gt; setting and add a new &lt;strong&gt;run script phase&lt;/strong&gt;. The “&lt;em&gt;Run script&lt;/em&gt;” action will be on the bottom and you need to move it above the “&lt;em&gt;Copy bundle resources&lt;/em&gt;” action.&lt;/p&gt;

&lt;p&gt;When you move it, you can paste this code for the script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;GIT&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;xcrun &lt;span class="nt"&gt;-find&lt;/span&gt; git&lt;span class="sb"&gt;`&lt;/span&gt;
&lt;span class="nv"&gt;GIT_REV&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;GIT&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt; rev-parse &lt;span class="nt"&gt;--short&lt;/span&gt; HEAD&lt;span class="sb"&gt;`&lt;/span&gt;
/usr/libexec/PlistBuddy &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s2"&gt;"Set :REVISION &lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;GIT_REV&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;SRCROOT&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;/{{path-to-your-info.plist-file}}"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the script above we rely on &lt;strong&gt;PlistBuddy&lt;/strong&gt; to set the key &lt;code&gt;REVISION&lt;/code&gt; and the value of the latest git commit hash to the app’s &lt;code&gt;Info.plist&lt;/code&gt; file. &lt;strong&gt;PlistBuddy&lt;/strong&gt; is a macOS utility that helps us to manage plist files. You can read more about it &lt;a href="https://www.manpagez.com/man/8/PlistBuddy/"&gt;here&lt;/a&gt; or you can type &lt;code&gt;/usr/libexec/PlistBuddy -h&lt;/code&gt; in your terminal to find out more of what it can do.&lt;/p&gt;

&lt;p&gt;Now, when you have run the build process, the script that we added should’ve inserted a new entry in your &lt;code&gt;Info.plist&lt;/code&gt; file and we can proceed to displaying it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Displaying the revision
&lt;/h2&gt;

&lt;p&gt;To display the revision we just need to fetch our &lt;code&gt;Info.plist&lt;/code&gt; and provide a key to read the value from it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;revision&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Bundle&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;object&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;forInfoDictionaryKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"REVISION"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as?&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Objective-C we would do it like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight objective_c"&gt;&lt;code&gt;&lt;span class="n"&gt;NSString&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;revision&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;NSBundle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;mainBundle&lt;/span&gt; &lt;span class="nf"&gt;objectForInfoDictionaryKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s"&gt;@"REVISION"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>ios</category>
      <category>git</category>
      <category>xcode</category>
    </item>
    <item>
      <title>Display SwiftUI views from Objective-C codebase</title>
      <dc:creator>Tarik Dahic</dc:creator>
      <pubDate>Thu, 18 Nov 2021 18:44:55 +0000</pubDate>
      <link>https://dev.to/daholino/display-swiftui-views-from-objective-c-codebase-15o</link>
      <guid>https://dev.to/daholino/display-swiftui-views-from-objective-c-codebase-15o</guid>
      <description>&lt;p&gt;&lt;strong&gt;SwiftUI&lt;/strong&gt; is getting more mature with every new major iOS release and it is now in a state where it can be used for a lot of use cases. The app that I work on my daily job is based on Objective-C and I wanted to prototype some things in SwiftUI because it was &lt;strong&gt;faster&lt;/strong&gt; and &lt;strong&gt;more fun&lt;/strong&gt;. It was also a great way to escape &lt;strong&gt;UIKit&lt;/strong&gt; for a little and learn more about SwiftUI. That means I had to display SwiftUI views from Objective-C ViewControllers. In this article I will explain how I did that.&lt;/p&gt;

&lt;p&gt;There are some limitations between Objective-C and Swift regarding SwiftUI. SwiftUI views are structs and they are not visible from the Objective-C code. The only way to bridge the two worlds is to use some kind of a mediator class between Objective-C and Swift code.&lt;/p&gt;

&lt;h2&gt;
  
  
  SwiftUI and UIKit
&lt;/h2&gt;

&lt;p&gt;Because we will be displaying SwiftUI views from UIKit we first need to know what abstractions to use to wrap SwiftUI views. UIKit can only present or push &lt;code&gt;UIViewController&lt;/code&gt; instances. To create &lt;code&gt;UIViewController&lt;/code&gt; instance from SwiftUI view we need to wrap it into a &lt;code&gt;UIHostingController&lt;/code&gt;. In &lt;code&gt;UIHostingController&lt;/code&gt; initializer we pass the SwiftUI view that we want to wrap like in the example below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;controller&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;UIHostingController&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;rootView&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;SwiftUIView&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;UIHostingController&lt;/code&gt; is a subclass of &lt;code&gt;UIViewController&lt;/code&gt; and now &lt;code&gt;controller&lt;/code&gt; variable can be used from our UIKit code to present it, push it onto the navigation stack or add it as a child view controller. You can read more about &lt;code&gt;UIHostingController&lt;/code&gt; &lt;a href="https://developer.apple.com/documentation/swiftui/uihostingcontroller"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding Obj-C to the mix
&lt;/h2&gt;

&lt;p&gt;Now that we know how to display SwiftUI views inside UIKit we need to bridge &lt;code&gt;UIHostingController&lt;/code&gt; to the Objective-C world. As I mentioned earlier, Swift structs are not visible from the Objective-C context so we need to add a middleman that will create &lt;code&gt;UIHostingController&lt;/code&gt; instances wrapped around SwiftUI views.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I like to call the middleman implementations as SwiftUIViewAdapter or SwiftUIViewFactory. I am still not settled on a final suffix but something between these two seems right.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let’s start with a simple SwiftUI view that will display the text that is passed into its initializer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;import&lt;/span&gt; &lt;span class="kt"&gt;SwiftUI&lt;/span&gt;

&lt;span class="kd"&gt;struct&lt;/span&gt; &lt;span class="kt"&gt;HelloWorldView&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;View&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;

    &lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;some&lt;/span&gt; &lt;span class="kt"&gt;View&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To display the &lt;code&gt;HelloWorldView&lt;/code&gt; we need to wrap it into the middleman class that will create the &lt;code&gt;UIHostingController&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;@objc&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;HelloWorldViewFactory&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;NSObject&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;@objc&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;UIViewController&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;helloWorldView&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;HelloWorldView&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;hostingController&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;UIHostingController&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;rootView&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;helloWorldView&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;hostingController&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are a couple of things happening in the &lt;code&gt;HelloWorldViewFactory&lt;/code&gt; class implementation above, so let’s cover them one by one:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;HelloWorldViewFactory&lt;/code&gt; class has one static function &lt;code&gt;create(text:)&lt;/code&gt; that will create the &lt;code&gt;HelloWorldView&lt;/code&gt; and initialize it, wrap it inside the &lt;code&gt;UIHostingController&lt;/code&gt; and return it.&lt;/li&gt;
&lt;li&gt;To expose &lt;code&gt;HelloWorldViewFactory&lt;/code&gt; to Objective-C code we need to prefix it with &lt;code&gt;@objc&lt;/code&gt; annotation and we need to subclass &lt;code&gt;NSObject&lt;/code&gt;. This is because &lt;code&gt;NSObject&lt;/code&gt; is the root class of most Objective-C class hierarchies, from which subclasses inherit a basic interface to the runtime system and the ability to behave as Objective-C objects.&lt;/li&gt;
&lt;li&gt;You can also notice that we are passing &lt;em&gt;text&lt;/em&gt; String argument to &lt;code&gt;create&lt;/code&gt; function. This allows us to use custom initializers for SwiftUI views and to pass dependencies that views need.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now we can display this view from Objective-C like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight objective_c"&gt;&lt;code&gt;&lt;span class="n"&gt;UIViewController&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;vc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;HelloWorldViewFactory&lt;/span&gt; &lt;span class="nf"&gt;createWithText&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s"&gt;@"Hello from Obj-C!"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt; &lt;span class="nf"&gt;presentViewController&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;vc&lt;/span&gt; &lt;span class="nf"&gt;animated&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nb"&gt;YES&lt;/span&gt; &lt;span class="n"&gt;completion&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="nb"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example the device should display the view that has text centered and set to &lt;em&gt;Hello from Obj-C!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CmqidIAj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://tarikdahic.com/images/swiftui-from-objc-mockup.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CmqidIAj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://tarikdahic.com/images/swiftui-from-objc-mockup.png" alt="" width="428" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Importing Swift code into Objective-C
&lt;/h4&gt;

&lt;p&gt;If you didn’t use Swift in your existing Objective-C project it is important to know that you need to import the Swift module header into the Objective-C file that will use your Swift code. That header file is named like &lt;code&gt;$(SWIFT_MODULE_NAME)-Swift.h&lt;/code&gt; and if you want, you change the name for that header file inside build settings for your target. The config option that defines the name is &lt;em&gt;Objective-C Generated Interface Header Name&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;If your app target is called &lt;code&gt;MyTestApp&lt;/code&gt; you can import the Swift code into Objective-C like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight objective_c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#import "MyTestApp-Swift.h"
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you are in the same situation as I was I hope that this article will help you.&lt;/p&gt;

</description>
      <category>ios</category>
      <category>swift</category>
      <category>swiftui</category>
      <category>objc</category>
    </item>
    <item>
      <title>Using semaphores in iOS to serialise callback completion</title>
      <dc:creator>Tarik Dahic</dc:creator>
      <pubDate>Fri, 12 Nov 2021 10:11:57 +0000</pubDate>
      <link>https://dev.to/daholino/using-semaphores-in-ios-to-serialise-callback-completion-15ce</link>
      <guid>https://dev.to/daholino/using-semaphores-in-ios-to-serialise-callback-completion-15ce</guid>
      <description>&lt;p&gt;In most of the situations when you are developing apps you will encounter APIs provided to you by someone else (framework, library, system, etc.). You cannot change the API and you need to use it as it is. Let’s say, to use that API, you give it a function that will be called when the result is ready or some action is completed (also known as a &lt;em&gt;callback&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;But, what if you don’t want to continue if the result is not ready or action is not completed? I’ve found that using &lt;strong&gt;semaphores&lt;/strong&gt; is good for this situation and I will describe how to do it in this article. But first, we need to learn more about semaphores.&lt;/p&gt;

&lt;h2&gt;
  
  
  Semaphore 🚦
&lt;/h2&gt;

&lt;p&gt;Semaphore mechanism was proposed by Dijkstra in the sixties which is a very significant technique to manage concurrent operations on shared resources. A trivial semaphore is a variable, like &lt;em&gt;integer&lt;/em&gt; or a &lt;em&gt;boolean&lt;/em&gt; that is incremented/decremented or toggled and depending on the state of that variable threads can access the resource or wait for it to become free.&lt;/p&gt;

&lt;p&gt;A useful way to think of a semaphore as used in a real-world system is as a record of how many units of a particular resource are available, coupled with operations to adjust that record &lt;em&gt;safely&lt;/em&gt; (i.e., to avoid &lt;a href="https://en.wikipedia.org/wiki/Race_condition"&gt;race conditions&lt;/a&gt;) as units are acquired or become free, and, if necessary, wait until a unit of the resource becomes available.&lt;/p&gt;

&lt;p&gt;There are two types of semaphores:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Binary Semaphore&lt;/strong&gt; — It can have only two values – 0 and 1, and we can also represent this semaphore with a boolean. It will allow only one thread to use the resource while others will wait.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Counting Semaphore&lt;/strong&gt; — By creating this semaphore we provide it with a number of how many threads are allowed to use it at the same time. When threads use the resource the semaphore counter is decremented. If the counter reaches 0 the threads that want to access the resource will wait until one of the threads is finished.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If we think of the implementation, a simple semaphore could be implemented by using one &lt;strong&gt;integer&lt;/strong&gt; variable and one &lt;strong&gt;FIFO queue&lt;/strong&gt;. Initial count of the integer variable determines how many available resources we have for consumers to use. If someone asks for access we decrement the &lt;strong&gt;count&lt;/strong&gt; variable and if the value is not negative we allow the access to the resource. If the value is negative we add that consumer to FIFO queue and halt it until one of the consumers that is already using the shared resource signals that it has finished. When the thread &lt;strong&gt;signals&lt;/strong&gt; that it has finished using the shared resource we increment the count variable and if threads are waiting in the FIFO queue we use the first one and give it access to the shared resource. Implementation like this where there is no prioritisation of threads can lead to a problem called &lt;a href="https://en.wikipedia.org/wiki/Priority_inversion"&gt;priority inversion&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The operation that consumers use to request access of the shared resource which decrements the count variable is called &lt;strong&gt;wait&lt;/strong&gt; and the operation of incrementing the count variable and telling the semaphore that we’re done using the shared resource is called &lt;strong&gt;signal&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Another great analogy that I found is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Semaphore is the number of free identical toilet keys. Example, say we have four toilets with identical locks and keys. The semaphore count — the count of keys — is set to 4 at beginning (all four toilets are free), then the count value is decremented as people are coming in. If all toilets are full, ie. there are no free keys left, the semaphore count is 0. Now, when eq. one person leaves the toilet, semaphore is increased to 1 (one free key), and given to the next person in the queue.&lt;/p&gt;

&lt;p&gt;Officially: “A semaphore restricts the number of simultaneous users of a shared resource up to a maximum number. Threads can request access to the resource (decrementing the semaphore), and can signal that they have finished using the resource (incrementing the semaphore).”&lt;em&gt;Ref: Symbian Developer Library&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Semaphores in iOS
&lt;/h2&gt;

&lt;p&gt;In iOS semaphore implementation is a part of the &lt;code&gt;Dispatch&lt;/code&gt; framework. You can use semaphores with &lt;em&gt;Obj-C&lt;/em&gt; and &lt;em&gt;Swift&lt;/em&gt;. I will provide an example for both languages.&lt;/p&gt;

&lt;p&gt;The API to use semaphores is not complicated and it is very &lt;em&gt;straightforward&lt;/em&gt;. We have 3 operations: &lt;em&gt;create, wait&lt;/em&gt; and &lt;em&gt;signal&lt;/em&gt;. In semaphore implementation for iOS the &lt;em&gt;wait&lt;/em&gt; operation can be requested with a &lt;strong&gt;timeout&lt;/strong&gt; to limit how much we want to wait to resource to become free. I advise to always provide a timeout so that the threads don’t become blocked.&lt;/p&gt;

&lt;p&gt;To create the semaphores we can use the code below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Swift&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;semaphore&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;DispatchSemaphore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// Obj-C&lt;/span&gt;
&lt;span class="n"&gt;dispatch_semaphore_t&lt;/span&gt; &lt;span class="n"&gt;semaphore&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;dispatch_semaphore_create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, if we want to &lt;strong&gt;wait&lt;/strong&gt; and &lt;strong&gt;signal&lt;/strong&gt; we can use the operations below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Wait - Swift&lt;/span&gt;
&lt;span class="n"&gt;semaphore&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;wait&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// Wait - Obj-C&lt;/span&gt;
&lt;span class="nf"&gt;dispatch_semaphore_wait&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;semaphore&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;dispatch_time&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;DISPATCH_TIME_NOW&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;int64_t&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="mf"&gt;5.0&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="kt"&gt;NSEC_PER_SEC&lt;/span&gt;&lt;span class="p"&gt;)));&lt;/span&gt;

&lt;span class="c1"&gt;// Signal - Swift&lt;/span&gt;
&lt;span class="n"&gt;semaphore&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;// Signal - Obj-C&lt;/span&gt;
&lt;span class="nf"&gt;dispatch_semaphore_signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;semaphore&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Serialising callback completion
&lt;/h2&gt;

&lt;p&gt;While dealing with some async APIs that take completion handlers I’ve run in to a problem where I need to wait for the execution of that API call to complete so that I can continue with the execution of my program. I didn’t want to end the function that is currently running and I used semaphores to wait for the async API to complete.&lt;/p&gt;

&lt;p&gt;This has proven very useful to me when I work on &lt;em&gt;iOS App Extensions&lt;/em&gt; that run in the background and that have very limited time of execution. While dealing with &lt;code&gt;UNUserNotificationCenter&lt;/code&gt; that provides async operations this technique helped me a lot. So, without further ado, let’s go to the example.&lt;/p&gt;

&lt;p&gt;In the code below I have a function that takes another function as a parameter and runs it after &lt;em&gt;the work is completed&lt;/em&gt;. This is just a simulation of what other real-world APIs provide.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;doSomethingAndNotifyMe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;via&lt;/span&gt; &lt;span class="nv"&gt;completion&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;@escaping&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;DispatchQueue&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;global&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;async&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// some work is being done here :)&lt;/span&gt;
        &lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;completion&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I will use &lt;code&gt;doSomethingAndNotifyMe&lt;/code&gt; in the example below to see what happens and how my code executes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"1. Starting the work"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;doSomethingAndNotifyMe&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"2. Work done"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"3. End of the work"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After running the example above, I get this output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. Starting the work
3. End of the work
2. Work done
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can see that the output messages are not in the order I want them to be, they are ordered as 1, 3, 2 and I want the order to be 1, 2, 3. To have the order that I want I will introduce semaphore to the example above:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"1. Starting the work"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;semaphore&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;DispatchSemaphore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;doSomethingAndNotifyMe&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"2. Work done"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;semaphore&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;semaphore&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;wait&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"3. End of the work"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When I run the example with the semaphore I get the output that I want:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. Starting the work
2. Work done
3. End of the work
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can see that I initialised the semaphore with the value &lt;code&gt;0&lt;/code&gt;. This is not a practice that is commonly seen and I like to think of this type of a semaphore as a &lt;strong&gt;blocking semaphore&lt;/strong&gt;. You can think of it as using a lock where the default state of the lock is &lt;em&gt;locked&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Wait operation can be &lt;strong&gt;dangerous&lt;/strong&gt; so don’t use it on the main thread and always try to provide the &lt;strong&gt;timeout&lt;/strong&gt; for it.&lt;/p&gt;

&lt;p&gt;Although this might not be the best practice for this kind of situations it has really proven to work great. A modern alternative to using semaphores for examples like this is using &lt;code&gt;DispatchGroup&lt;/code&gt; API from the &lt;code&gt;Dispatch&lt;/code&gt; library. &lt;code&gt;DispatchGroup&lt;/code&gt; is only available on Apple platforms while semaphore implementations are available for almost every platform.&lt;/p&gt;

&lt;p&gt;Have you used semaphores in your projects or any other mechanisms for dealing with multi-threaded environments? Please share your experiences and thank you for reading this article.&lt;/p&gt;

</description>
      <category>ios</category>
      <category>swift</category>
      <category>objc</category>
    </item>
    <item>
      <title>Using Network Link Conditioner to simulate bad network conditions on iOS and macOS</title>
      <dc:creator>Tarik Dahic</dc:creator>
      <pubDate>Thu, 04 Nov 2021 19:53:57 +0000</pubDate>
      <link>https://dev.to/daholino/using-network-link-conditioner-to-simulate-bad-network-conditions-on-ios-and-macos-1a4h</link>
      <guid>https://dev.to/daholino/using-network-link-conditioner-to-simulate-bad-network-conditions-on-ios-and-macos-1a4h</guid>
      <description>&lt;p&gt;&lt;strong&gt;Network Link Conditioner&lt;/strong&gt; is a network utility tool provided by Apple that can simulate various network conditions and help us to develop better apps. If you ever received a bug report where user mentioned slow network connection the Network Link Conditioner will help you to reproduce that issue.&lt;/p&gt;

&lt;p&gt;We are always on the go, using our mobile devices in different places with different network conditions and we need to adjust the apps that we develop for that conditions. For example, the elevators are ruthless, somehow the app that I work on has always issues with them! 😀&lt;/p&gt;

&lt;h2&gt;
  
  
  Installation
&lt;/h2&gt;

&lt;p&gt;Network Link Conditioner is available on iOS and macOS and in this article I will cover how to install and use it on both platforms.&lt;/p&gt;

&lt;h3&gt;
  
  
  iOS
&lt;/h3&gt;

&lt;p&gt;To enable Network Link Conditioner on iOS device you will need to enable that device for &lt;strong&gt;development&lt;/strong&gt;. You can do that by connecting and building the app directly to your device. If your device is not enabled for development, Xcode will do that before building and installing your app.&lt;/p&gt;

&lt;p&gt;The other option is to go to &lt;em&gt;Window&lt;/em&gt; → &lt;em&gt;Devices and Simulators&lt;/em&gt; in Xcode and from there select your device and enable it for development.&lt;/p&gt;

&lt;p&gt;Inside the &lt;em&gt;Settings&lt;/em&gt; app you will now have a &lt;em&gt;Developer&lt;/em&gt; section and inside that section you will be able to use Network Link Conditioner.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Ftarikdahic.com%2Fimages%2Fnetwork-link-conditioner-ios.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Ftarikdahic.com%2Fimages%2Fnetwork-link-conditioner-ios.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  macOS
&lt;/h3&gt;

&lt;p&gt;You will need to go to &lt;em&gt;more downloads&lt;/em&gt; page on the Apple Developer portal to and search for appropriate &lt;strong&gt;Additional Tools&lt;/strong&gt; for your Xcode version. &lt;a href="https://developer.apple.com/download/all/?q=Additional%20Tools" rel="noopener noreferrer"&gt;This&lt;/a&gt; link will take you directly to those search results. Download the &lt;code&gt;dmg&lt;/code&gt; file, open it, and inside &lt;em&gt;Hardware&lt;/em&gt; folder you will find the file called &lt;code&gt;Network Link Conditioner.prefPane&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Ftarikdahic.com%2Fimages%2Fnetwork-link-conditioner-install.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Ftarikdahic.com%2Fimages%2Fnetwork-link-conditioner-install.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Double click on it to install it and when the installation is completed you can open the &lt;strong&gt;Network Link Conditioner&lt;/strong&gt; by going to the &lt;em&gt;Settings&lt;/em&gt; app and at the bottom of the window you will find it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Ftarikdahic.com%2Fimages%2Fnetwork-link-conditioner-settings.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Ftarikdahic.com%2Fimages%2Fnetwork-link-conditioner-settings.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How to use
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Network Link Conditioner&lt;/strong&gt; comes with a lot of presets that you can use like &lt;em&gt;3G, Edge, Very Bad Network, LTE, 100% Loss&lt;/em&gt; and more. They are great for testing your app on bad network conditions and in most cases you will not need to create custom presets (profiles).&lt;/p&gt;

&lt;p&gt;To use it, pick a preset and just &lt;em&gt;flip the switch&lt;/em&gt; to enable it. It is that easy, but don’t forget to turn it off after you are done with testing because it is applied on the entire system.&lt;/p&gt;

&lt;p&gt;Every preset has following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Downlink&lt;/strong&gt; bandwidth, packet drop rate and delay in ms&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Uplink&lt;/strong&gt; bandwidth, packet drop rate and delay in ms&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;DNS Delay&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By tweaking these attributes you can simulate a lot of different network conditions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Edge&lt;/strong&gt; is a great preset to see how your app behaves on very slow networks. &lt;strong&gt;Very Bad Network&lt;/strong&gt; preset is great when you want to test that you have connection to the Internet but that it is very unreliable.&lt;/p&gt;

&lt;p&gt;I am working with VoIP and I have found that custom profile with &lt;em&gt;60%&lt;/em&gt; packet loss in both directions is very great for testing reconnection and recovery mechanisms.&lt;/p&gt;

&lt;p&gt;If you are using &lt;strong&gt;iOS Simulator&lt;/strong&gt; you will need to install the Network Link Conditioner on macOS and apply it to entire system. This will affect the connection of your entire macOS.&lt;/p&gt;

&lt;p&gt;Here’s a little trick that I use for uploading. When I need to upload a large file and I don’t want to make my home network unusable I just turn on Network Link Conditioner with custom profile that limits my upload speed. It is configured like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Ftarikdahic.com%2Fimages%2Fnetwork-link-conditioner-upload-limit.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Ftarikdahic.com%2Fimages%2Fnetwork-link-conditioner-upload-limit.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Network Link Conditioner&lt;/strong&gt; is a very useful tool and if you are not using it to test your apps your users might suffer! If you have any questions, please post them in the comments below.&lt;/p&gt;

</description>
      <category>ios</category>
      <category>xcode</category>
    </item>
    <item>
      <title>Build iOS apps from the command line using xcodebuild</title>
      <dc:creator>Tarik Dahic</dc:creator>
      <pubDate>Wed, 27 Oct 2021 18:06:46 +0000</pubDate>
      <link>https://dev.to/daholino/build-ios-apps-from-the-command-line-using-xcodebuild-47i2</link>
      <guid>https://dev.to/daholino/build-ios-apps-from-the-command-line-using-xcodebuild-47i2</guid>
      <description>&lt;p&gt;At some point in your iOS development career you will want to automate app building and distribution when there are new changes on your version control system. At the company I work for we did just that - whenever a pull request is merged our CI/CD system is notified and it starts to build and test the app with the newest changes. The successful builds are then deployed to QAs and they are ready for manual testing and verification. This can save you a lot of time in your development workflow. In this article I will go through the process on how to create &lt;strong&gt;.ipa&lt;/strong&gt; file from your project that is ready for testing or distribution using only terminal.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tools
&lt;/h2&gt;

&lt;p&gt;You will need to have &lt;em&gt;Xcode Command Line Tools&lt;/em&gt; installed on the machine that will be building the app. If you already have Xcode it is very likely that you have &lt;em&gt;Xcode Command Line Tools&lt;/em&gt; installed. Specifically, we will be using the &lt;strong&gt;xcodebuild&lt;/strong&gt; tool . To see if you have it installed you can run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;xcodebuild &lt;span class="nt"&gt;-help&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you have Xcode and don’t have this tool you can install it by running this in the terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;xcode-select &lt;span class="nt"&gt;--install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or you can download the command line tools from &lt;a href="https://developer.apple.com/download/all/" rel="noopener noreferrer"&gt;developer.apple.com&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building
&lt;/h2&gt;

&lt;p&gt;Building iOS apps is a two step process. First we compile and link the compiled code using &lt;strong&gt;archive&lt;/strong&gt; step and then we &lt;strong&gt;export&lt;/strong&gt; and sign that archive to create the &lt;em&gt;.ipa&lt;/em&gt; file.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Ftarikdahic.com%2Fimages%2Fxcodebuild-build-process.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Ftarikdahic.com%2Fimages%2Fxcodebuild-build-process.png" alt="Build process"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Choosing what to build
&lt;/h4&gt;

&lt;p&gt;Open the terminal and navigate to the root of the folder of your project. We need to know what will we build so let’s see what options we have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# If you are using Xcode project:&lt;/span&gt;
xcodebuild &lt;span class="nt"&gt;-list&lt;/span&gt; &lt;span class="nt"&gt;-project&lt;/span&gt; &amp;lt;project-name&amp;gt;.xcodeproj
&lt;span class="c"&gt;# or if you are using Xcode workspace:&lt;/span&gt;
xcodebuild &lt;span class="nt"&gt;-list&lt;/span&gt; &lt;span class="nt"&gt;-workspace&lt;/span&gt; &amp;lt;project-name&amp;gt;.xcworkspace
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should get information about available targets, build configurations and schemes. Schemes contain data about which targets and configurations to use when building so remember what scheme you want to use.&lt;/p&gt;

&lt;h4&gt;
  
  
  Clean
&lt;/h4&gt;

&lt;p&gt;Before building anything that will be distributed internally or externally I always like to perform &lt;em&gt;clean&lt;/em&gt; operation. You can do that via &lt;em&gt;xcodebuild&lt;/em&gt; like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;xcodebuild clean
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Archive
&lt;/h4&gt;

&lt;p&gt;The first step in the actual building is to create an app &lt;strong&gt;archive&lt;/strong&gt;. It will compile your code and sign it with you development certificate. This step is analogous to &lt;em&gt;Archiving&lt;/em&gt; via Xcode. Xcode generated archives can be accessed in Xcode’s &lt;em&gt;Organizer&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;xcodebuild archive &lt;span class="nt"&gt;-scheme&lt;/span&gt; &amp;lt;scheme-that-you-want-to-use&amp;gt; &lt;span class="nt"&gt;-sdk&lt;/span&gt; iphoneos &lt;span class="nt"&gt;-allowProvisioningUpdates&lt;/span&gt; &lt;span class="nt"&gt;-archivePath&lt;/span&gt; &amp;lt;path-and-name-for-archive&amp;gt;.xcarchive
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above I included the flag &lt;strong&gt;-allowProvisioningUpdates&lt;/strong&gt; and this will generate or modify provisioning profiles if you use &lt;em&gt;Automatic signing&lt;/em&gt;. This option is not very reliable and in most of my apps I’ve downloaded and imported manually provisioning profiles or certificates to Keychain.&lt;/p&gt;

&lt;p&gt;Documentation for &lt;em&gt;allowProvisioningUpdates&lt;/em&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Allow xcodebuild to communicate with the Apple Developer website. For automatically signed targets, xcodebuild will create and update profiles, app IDs, and certificates. For manually signed targets, xcodebuild will download missing or updated provisioning profiles. Requires a developer account to have been added in Xcode’s Accounts preference pane or an App Store Connect authentication key to be specified via the -authenticationKeyPath, -authenticationKeyID, and -authenticationKeyIssuerID parameters.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Because I am building for iOS I provided &lt;code&gt;-sdk iphoneos&lt;/code&gt; and if you want to see what other sdks are available or if you want to build your app for another platform you can use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;xcodebuild &lt;span class="nt"&gt;-showsdks&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Export
&lt;/h4&gt;

&lt;p&gt;In export step we need to decide how are we gonna distribute the &lt;strong&gt;.ipa&lt;/strong&gt; file that we will generate and we need to sign it with &lt;strong&gt;distribution certificate&lt;/strong&gt;. If you already archived and exported the app from your computer using Xcode you have the distribution certificate in Keychain and you will be able to complete this step. If you don’t have the distribution certificate you can generate a new one or you can export existing one from your development computer and import it into the computer that will be doing the export step.&lt;/p&gt;

&lt;p&gt;We can run the export operation with the command below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;xcodebuild &lt;span class="nt"&gt;-exportArchive&lt;/span&gt; &lt;span class="nt"&gt;-archivePath&lt;/span&gt; &amp;lt;path-and-name-for-archive-from-the-previous-step&amp;gt;.xcarchive &lt;span class="nt"&gt;-exportOptionsPlist&lt;/span&gt; exportOptions.plist &lt;span class="nt"&gt;-exportPath&lt;/span&gt; &amp;lt;path-where-ipa-file-will-be-saved&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The export options &lt;em&gt;plist&lt;/em&gt; file plays an important role in this step. It is an xml formatted file that contains all the necessary information for &lt;code&gt;xcodebuild&lt;/code&gt; so that the &lt;em&gt;.ipa&lt;/em&gt; file can be generated.&lt;/p&gt;

&lt;h5&gt;
  
  
  Export options
&lt;/h5&gt;

&lt;p&gt;If you don’t know how to generate your export options plist file you can see how Xcode does it for your app. For example, archive the app inside Xcode and then proceed with exporting it from the Xcode Organizer. Choose &lt;code&gt;Ad-Hoc&lt;/code&gt; distribution and export it wherever you like. Navigate to the folder where your app &lt;em&gt;.ipa&lt;/em&gt; file was exported and in it you will find the &lt;strong&gt;ExportOptions.plist&lt;/strong&gt; file that Xcode internally generated. This could be a great starting point for manually building your apps because we can use that file for builds from command line.&lt;/p&gt;

&lt;p&gt;This is how the export options file looked for one of my apps:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;&amp;lt;!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;plist&lt;/span&gt; &lt;span class="na"&gt;version=&lt;/span&gt;&lt;span class="s"&gt;"1.0"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;dict&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;key&amp;gt;&lt;/span&gt;compileBitcode&lt;span class="nt"&gt;&amp;lt;/key&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;true/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;key&amp;gt;&lt;/span&gt;destination&lt;span class="nt"&gt;&amp;lt;/key&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;string&amp;gt;&lt;/span&gt;export&lt;span class="nt"&gt;&amp;lt;/string&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;key&amp;gt;&lt;/span&gt;method&lt;span class="nt"&gt;&amp;lt;/key&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;string&amp;gt;&lt;/span&gt;ad-hoc&lt;span class="nt"&gt;&amp;lt;/string&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;key&amp;gt;&lt;/span&gt;signingStyle&lt;span class="nt"&gt;&amp;lt;/key&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;string&amp;gt;&lt;/span&gt;automatic&lt;span class="nt"&gt;&amp;lt;/string&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;key&amp;gt;&lt;/span&gt;stripSwiftSymbols&lt;span class="nt"&gt;&amp;lt;/key&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;true/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;key&amp;gt;&lt;/span&gt;teamID&lt;span class="nt"&gt;&amp;lt;/key&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;string&amp;gt;&lt;/span&gt;YOUR_TEAM_ID&lt;span class="nt"&gt;&amp;lt;/string&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;key&amp;gt;&lt;/span&gt;thinning&lt;span class="nt"&gt;&amp;lt;/key&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;string&amp;gt;&lt;/span&gt;&lt;span class="ni"&gt;&amp;amp;lt;&lt;/span&gt;none&lt;span class="ni"&gt;&amp;amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/string&amp;gt;&lt;/span&gt;
    &lt;span class="c"&gt;&amp;lt;!-- If you need to provide provisioning profiles you can do it like this: --&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;key&amp;gt;&lt;/span&gt;provisioningProfiles&lt;span class="nt"&gt;&amp;lt;/key&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;dict&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;key&amp;gt;&lt;/span&gt;APP_IDENTIFIER&lt;span class="nt"&gt;&amp;lt;/key&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;string&amp;gt;&lt;/span&gt;Ad Hoc Distribution&lt;span class="nt"&gt;&amp;lt;/string&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/dict&amp;gt;&lt;/span&gt;
    &lt;span class="c"&gt;&amp;lt;!-- I didn't need to provide provisioning profiles for this app 
         because its signing style is set to automatic --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/dict&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/plist&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are a lot of more options which you can find by running &lt;code&gt;xcodebuild -help&lt;/code&gt;.&lt;/p&gt;

&lt;h5&gt;
  
  
  Export method
&lt;/h5&gt;

&lt;p&gt;Documentation for export method from &lt;code&gt;xcodebuild&lt;/code&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Describes how Xcode should export the archive. Available options: app-store, validation, ad-hoc, package, enterprise, development, developer-id, and mac-application. The list of options varies based on the type of archive. Defaults to development.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Mostly used ones for iOS are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;app-store:&lt;/strong&gt; Generates an &lt;em&gt;.ipa&lt;/em&gt; signed with your distribution certificate that is ready for production or deploying to TestFlight.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ad-hoc:&lt;/strong&gt; Generates an &lt;em&gt;.ipa&lt;/em&gt; signed with Ad-Hoc distribution certificate which will enable users that have their device identifiers registered in your Developer portal to install the app.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;enterprise:&lt;/strong&gt; Generates an &lt;em&gt;.ipa&lt;/em&gt; signed with your Enterprise Distribution certificate which will enable you to distribute the app in your organisation. For this option you need to have Enterprise Apple developer account.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;development:&lt;/strong&gt; Generates an &lt;em&gt;.ipa&lt;/em&gt; signed with your Development certificate which will enable your account members to install and use the app.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At the company I work for we use &lt;code&gt;Ad-Hoc&lt;/code&gt; export method for distributing apps to our QAs and we use &lt;code&gt;enterprise&lt;/code&gt; export method for sending custom builds with debug options so that we can resolve tricky issues.&lt;/p&gt;

&lt;h4&gt;
  
  
  Building with different versions of Xcode
&lt;/h4&gt;

&lt;p&gt;In some cases we need to build apps with different version of Xcode. This might happen when a new Xcode with new iOS version and SDK is announced but there are some breaking changes that will take some time to fix. To build the app with an older Xcode version we need to change what Xcode our command line tool uses.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To see the current Xcode path we can enter this command:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;xcode-select &lt;span class="nt"&gt;-p&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;To change the path to custom version of Xcode we can enter this command:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;xcode-select &lt;span class="nt"&gt;-switch&lt;/span&gt; &amp;lt;path&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;​ where &lt;code&gt;&amp;lt;path&amp;gt;&lt;/code&gt; is the path to &lt;code&gt;Xcode.app&lt;/code&gt; package.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you want to verify that correct version is being used by command line tool you can use this command:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;xcodebuild &lt;span class="nt"&gt;-version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Issues that can happen
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;There might be an error with unverified &lt;em&gt;.framework&lt;/em&gt; files and to fix this issue, this command should be entered before the build process:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;spctl &lt;span class="nt"&gt;--master-disable&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Deploying and installing
&lt;/h2&gt;

&lt;p&gt;Now that you have the &lt;em&gt;.ipa&lt;/em&gt; file, you can upload it to App Store for production and TestFlight or you can distribute it to your organization or QAs.&lt;/p&gt;

&lt;h5&gt;
  
  
  Uploading to App Store
&lt;/h5&gt;

&lt;p&gt;You can upload the generated &lt;em&gt;.ipa&lt;/em&gt; file to App Store from terminal or using the &lt;a href="https://apps.apple.com/ba/app/transporter/id1450874784?mt=12" rel="noopener noreferrer"&gt;Transporter&lt;/a&gt;, an app from Apple that is made for uploading builds.&lt;/p&gt;

&lt;p&gt;Uploading from the command line can be done using &lt;code&gt;xcrun altool&lt;/code&gt;. To read more about it you can run &lt;code&gt;man altool&lt;/code&gt; and get the manual for using it. Example for uploading the app to the App Store using this tool coud look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;xcrun altool &lt;span class="nt"&gt;--upload-app&lt;/span&gt; &lt;span class="nt"&gt;--type&lt;/span&gt; ios &lt;span class="nt"&gt;--file&lt;/span&gt; &amp;lt;path-to-your-package-ipa&amp;gt; &lt;span class="nt"&gt;--username&lt;/span&gt; &amp;lt;user-name&amp;gt; &lt;span class="nt"&gt;--password&lt;/span&gt; &amp;lt;app-specific-password&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You need to provide the &lt;code&gt;altool&lt;/code&gt; with &lt;strong&gt;username&lt;/strong&gt; and &lt;strong&gt;app specific password&lt;/strong&gt;. The username is your Apple ID and app specific password can be generated in &lt;em&gt;App Store Connect&lt;/em&gt; for this app to authorize it.&lt;/p&gt;

&lt;h5&gt;
  
  
  Installing to devices
&lt;/h5&gt;

&lt;p&gt;This procedure depends on the operating system that the person who wants to install the app is using. The prerequisite for this step is to connect your iOS device to the computer.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;macOS users can use &lt;a href="https://apps.apple.com/us/app/apple-configurator-2/id1037126344?mt=12" rel="noopener noreferrer"&gt;Apple Configurator 2&lt;/a&gt; which is an app from Apple. This is my preferred way of installing the apps via macOS.&lt;/li&gt;
&lt;li&gt;Linux users can use &lt;a href="https://github.com/libimobiledevice/ideviceinstaller" rel="noopener noreferrer"&gt;ideviceinstaller&lt;/a&gt; which I tested and it worked great.&lt;/li&gt;
&lt;li&gt;Windows users can use &lt;a href="https://www.quamotion.mobi/docs/imobiledevice/" rel="noopener noreferrer"&gt;iMobileDevice&lt;/a&gt; builds for Windows. I haven’t tested this approach but after researching it a bit it seems to work good.&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  Distributing to your organisation
&lt;/h5&gt;

&lt;p&gt;If you have the Enterprise Apple Developer account the best way to distribute the apps is by using the manifest file that Xcode can generate for you when you export the app via Xcode. You can use that manifest file to host the &lt;em&gt;.ipa&lt;/em&gt; on some web server and allow the users of your organisation OTA updates.&lt;/p&gt;

&lt;p&gt;Manifest can also be generated when you use &lt;code&gt;xcodebuild&lt;/code&gt;. This is what the documentation says:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;manifest : Dictionary&lt;/p&gt;

&lt;p&gt;For non-App Store exports, users can download your app over the web by opening your distribution manifest file in a web browser. To generate a distribution manifest, the value of this key should be a dictionary with three sub-keys: appURL, displayImageURL, fullSizeImageURL. The additional sub-key assetPackManifestURL is required when using on-demand resources.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is a really great way to ease the app installation for enterprise users.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Although you might never need to build your apps manually from the command line because there are great tools like &lt;a href="https://fastlane.tools/" rel="noopener noreferrer"&gt;fastlane&lt;/a&gt; and &lt;a href="https://www.bitrise.io/" rel="noopener noreferrer"&gt;bitrise&lt;/a&gt; it will be very beneficial to understand what happens &lt;em&gt;under the hood&lt;/em&gt;. When things go wrong you will have the knowledge to fix the issues that happen by automating app building.&lt;/p&gt;

&lt;p&gt;By knowing this you can even create your own CI/CD tool. Add a &lt;code&gt;git hook&lt;/code&gt; to run tests and builds for your app in a script that contains the contents of this post, expose the generated &lt;em&gt;.ipa&lt;/em&gt; file via some web server and you have a great in-house solution!&lt;/p&gt;

&lt;p&gt;I hope that this article has helped you to understand how the build process for iOS apps work. If I forgot to add something or made a mistake somewhere please let me know in the comments!&lt;/p&gt;

</description>
      <category>ios</category>
      <category>xcode</category>
      <category>automation</category>
      <category>tools</category>
    </item>
    <item>
      <title>The Holy Grail of UIKit: Delegate Pattern</title>
      <dc:creator>Tarik Dahic</dc:creator>
      <pubDate>Wed, 20 Oct 2021 19:15:00 +0000</pubDate>
      <link>https://dev.to/daholino/the-holy-grail-of-uikit-delegate-pattern-1den</link>
      <guid>https://dev.to/daholino/the-holy-grail-of-uikit-delegate-pattern-1den</guid>
      <description>&lt;p&gt;When I started learning to develop apps for iOS I stumbled upon the delegate pattern for the first time. Adopting &lt;em&gt;UITableViewDataSource&lt;/em&gt; and &lt;em&gt;UITableViewDelegate&lt;/em&gt; protocols felt very strange and confusing.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Why am I doing this? Is this really how to do these things? I asked myself.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;After a couple of weeks of learning and reading materials, I clicked and really started to enjoy using this pattern.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is it?
&lt;/h2&gt;

&lt;p&gt;According to Apple:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Delegation is a simple and powerful pattern in which one object in a program acts on behalf of, or in coordination with, another object. The delegating object keeps a reference to the other object—the delegate—and at the appropriate time sends a message to it. The message informs the delegate of an event that the delegating object is about to handle or has just handled. The delegate may respond to the message by updating the appearance or state of itself or other objects in the application, and in some cases, it can return a value that affects how an impending event is handled. The main value of delegation is that it allows you to easily customize the behavior of several objects in one central object.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is a very good definition. But let’s take it step by step by following the illustration below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Ftarikdahic.com%2Fimages%2Fdelegate-illustration.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Ftarikdahic.com%2Fimages%2Fdelegate-illustration.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Someone creates a &lt;strong&gt;Worker&lt;/strong&gt;. A Worker is someone who does the work and then notifies us when the work is &lt;em&gt;completed&lt;/em&gt; or when something is &lt;em&gt;needed&lt;/em&gt; to continue the work.&lt;/li&gt;
&lt;li&gt;We tell the worker: “&lt;em&gt;This is your delegate. Ask it for something or notify it when you are done.&lt;/em&gt;”. That depends on the language that is known between the worker and its delegate. This is always known upfront.&lt;/li&gt;
&lt;li&gt;The Worker starts to work.&lt;/li&gt;
&lt;li&gt;While the Worker is working it needs some data to continue its work. The worker asks its delegate “&lt;em&gt;How should I do this or that?&lt;/em&gt;” and the delegate knowing what the worker needs provides it. When the Worker finishes the work it can tell its delegate “&lt;em&gt;I finished the work and here are the results!&lt;/em&gt;”.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is how the delegate pattern works. The communication flow is always 1-to-1 between the delegate and the worker.&lt;/p&gt;

&lt;p&gt;The worker is always doing the same job, but by providing different data and reacting differently on work completions we can reuse the worker in a lot of places where we need to handle the different cases.&lt;/p&gt;

&lt;p&gt;Delegation works great when there is a clear relationship between the owner and the “&lt;em&gt;Worker&lt;/em&gt;”. For other cases, you will want to resort to other patterns.&lt;/p&gt;

&lt;p&gt;I would also like to repeat that the worker knows what to send or request from the delegate.&lt;/p&gt;

&lt;h2&gt;
  
  
  Translating this into code
&lt;/h2&gt;

&lt;p&gt;After we’ve covered the basics with the theory we will try to translate this into code. I will be using Swift to implement this pattern.&lt;/p&gt;

&lt;h3&gt;
  
  
  Define the protocol
&lt;/h3&gt;

&lt;p&gt;We should always start with the delegate protocol, the interface or the language that will the delegate adopt and that the worker will know in advance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;protocol&lt;/span&gt; &lt;span class="kt"&gt;WorkerDelegate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;AnyObject&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;didFinishWork&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I declared &lt;em&gt;WorkerDelegate&lt;/em&gt; protocol that has one method for this example and that is &lt;em&gt;didFinishWork()&lt;/em&gt;. When the worker completes the work it will use the delegate that has conformance to &lt;em&gt;WorkerDelegate&lt;/em&gt; protocol and will call the &lt;em&gt;didFinishWork()&lt;/em&gt; method on it.&lt;/p&gt;

&lt;h4&gt;
  
  
  AnyObject
&lt;/h4&gt;

&lt;p&gt;You can notice that the &lt;em&gt;WorkerDelegate&lt;/em&gt; protocol conforms to another one and that is &lt;strong&gt;AnyObject&lt;/strong&gt;. We use AnyObject to limit that only class types can inherit our delegate protocol. Only class objects can be weak because they are passed by reference and we want weak delegates so that we don’t create &lt;strong&gt;retain cycles&lt;/strong&gt; while using delegates.&lt;/p&gt;

&lt;h3&gt;
  
  
  Define the worker
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;Worker&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;weak&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;delegate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;WorkerDelegate&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt;

    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;startWork&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Start some work..&lt;/span&gt;
        &lt;span class="c1"&gt;//&lt;/span&gt;
        &lt;span class="c1"&gt;//&lt;/span&gt;
        &lt;span class="c1"&gt;// .. and when it is completed, notify the delegate:&lt;/span&gt;
        &lt;span class="n"&gt;delegate&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;didFinishWork&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Worker class has a property &lt;strong&gt;delegate&lt;/strong&gt; that is of type &lt;em&gt;WorkerDelegate&lt;/em&gt;. It has a weak reference to something that will be stored in this variable and that is, as I mentioned above to avoid creating potential retain cycles. The delegate is also optional so by default it is &lt;strong&gt;nil&lt;/strong&gt;. This is useful in some cases where setting the delegate is not mandatory.&lt;/p&gt;

&lt;p&gt;The Worker has a &lt;em&gt;startWork()&lt;/em&gt; method that will start some work and when the work is completed it will notify its delegate by invoking the &lt;em&gt;didFinishWork()&lt;/em&gt; method if the delegate is set.&lt;/p&gt;

&lt;h3&gt;
  
  
  Create the worker and adopt the WorkerDelegate
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;ViewController&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UIViewController&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;worker&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Worker&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;viewDidLoad&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;viewDidLoad&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

        &lt;span class="c1"&gt;// It is always important to set the delegate before we invoke the worker to start doing something.&lt;/span&gt;
        &lt;span class="c1"&gt;// Better alternative could be to pass the delegate in the initializer of the Worker.&lt;/span&gt;
        &lt;span class="n"&gt;worker&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;delegate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;

        &lt;span class="n"&gt;worker&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;startWork&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;extension&lt;/span&gt; &lt;span class="kt"&gt;ViewController&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;WorkerDelegate&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;didFinishWork&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Worker finished the work. Now we can react to it by fetching some data or updating the UI.&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;em&gt;ViewController&lt;/em&gt; has a &lt;strong&gt;worker&lt;/strong&gt; property. In the &lt;em&gt;viewDidLoad()&lt;/em&gt; lifecycle method of the &lt;em&gt;ViewController&lt;/em&gt;, we set the delegate of the worker to &lt;em&gt;self&lt;/em&gt;, so that when the worker is done it will notify the &lt;em&gt;ViewController&lt;/em&gt; instance. After setting the delegate, we invoke the &lt;em&gt;startWork&lt;/em&gt;() method of the worker and we wait for it to complete to notify us via the &lt;em&gt;didFinishWork()&lt;/em&gt; method that we conformed to via &lt;em&gt;WorkerDelegate&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;I always like to conform to different protocols in the extensions of the classes because it improves the organisation and the readability of the code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Examples from UIKit
&lt;/h2&gt;

&lt;p&gt;While working with UIKit we use delegates on daily basis so this is why it is very important to understand this pattern. A good tip for you is when you need to conform to some delegate protocol is to open the protocol definition and see what it has to offer.&lt;/p&gt;

&lt;p&gt;Here are a couple of examples:&lt;/p&gt;

&lt;h4&gt;
  
  
  UITableViewDataSource
&lt;/h4&gt;

&lt;p&gt;We use &lt;em&gt;UITableViewDataSource&lt;/em&gt; to provide the &lt;em&gt;UITableView&lt;/em&gt; with data. A data source object responds to data-related requests from the table. The minimum for every &lt;em&gt;UITableView&lt;/em&gt; that we need to do is to provide it with the numbers of rows per section and to provide &lt;em&gt;UITableViewCell&lt;/em&gt; for every row. We do that by implementing methods from the &lt;em&gt;UITableViewDataSource&lt;/em&gt; protocol:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Return the number of rows for the table. We'll assume that the table has only one section.&lt;/span&gt;
&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;tableView&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;tableView&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UITableView&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;numberOfRowsInSection&lt;/span&gt; &lt;span class="nv"&gt;section&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Provide a cell object for each row&lt;/span&gt;
&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;tableView&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;tableView&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UITableView&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cellForRowAt&lt;/span&gt; &lt;span class="nv"&gt;indexPath&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;IndexPath&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;UITableViewCell&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="c1"&gt;// Fetch a cell of the appropriate type&lt;/span&gt;
   &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;cell&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tableView&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dequeueReusableCell&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;withIdentifier&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"cellTypeIdentifier"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;for&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;indexPath&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

   &lt;span class="c1"&gt;// Configure the cell using items[indexPath.row]&lt;/span&gt;

   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;cell&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example we can see how the &lt;em&gt;UITableView&lt;/em&gt; asks us (the data source delegate) for data that it will display.&lt;/p&gt;

&lt;h4&gt;
  
  
  UITableViewDelegate
&lt;/h4&gt;

&lt;p&gt;With &lt;em&gt;UITableViewDelegate&lt;/em&gt; we can react to some interactions with the &lt;em&gt;UITableView&lt;/em&gt;. In the example below we can override the &lt;em&gt;didSelectRowAt&lt;/em&gt; delegate method so that we get notified when user presses the &lt;em&gt;UITableView&lt;/em&gt; row.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;tableView&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;tableView&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UITableView&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;didSelectRowAt&lt;/span&gt; &lt;span class="nv"&gt;indexPath&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;IndexPath&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// React to the selection of the cell at the indexPath&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;UITableViewDelegate&lt;/em&gt; can also be used for more advanced &lt;em&gt;UITableView&lt;/em&gt; customization.&lt;/p&gt;

&lt;h4&gt;
  
  
  MFMailComposeViewControllerDelegate
&lt;/h4&gt;

&lt;p&gt;If we want to use native system UI and configuration to send an email we can use &lt;em&gt;MFMailComposeViewController&lt;/em&gt;. We will create the instance of &lt;em&gt;MFMailComposeViewController&lt;/em&gt;, configure it, adopts its delegate and send the email. The email completion will be delegated to us in the delegate method below that we implemented by overriding &lt;em&gt;MFMailComposeViewControllerDelegate&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;mailComposeController&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;MFMailComposeViewController&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;didFinishWith&lt;/span&gt; &lt;span class="nv"&gt;result&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;MFMailComposeResult&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;?)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// React to mail sending completion&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From here we can see if the email sending was successful or not and react according to that information.&lt;/p&gt;

&lt;h4&gt;
  
  
  UIImagePickerControllerDelegate
&lt;/h4&gt;

&lt;p&gt;If you want to use some images or videos from the Photos app you possibly implemented &lt;em&gt;UIImagePickerController&lt;/em&gt; and conformed to its protocol so that you know when the user picked the media files.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;imagePickerController&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;picker&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UIImagePickerController&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;didFinishPickingMediaWithInfo&lt;/span&gt; &lt;span class="nv"&gt;info&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;UIImagePickerController&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;InfoKey&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Any&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Use picked images or videos&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;The delegate pattern is very popular in the iOS world and it is very present in UIKit. I like this pattern and I use it in my work but it has some limitations. The communication is always 1-to-1 and that is not practical in some cases so we need to resort to something like an Observer pattern. The amount of code that needs to be added is not small if we are developing something trivial and the testing is harder.&lt;/p&gt;

&lt;p&gt;Alternative to using delegates can be passing functions to “&lt;em&gt;Workers&lt;/em&gt;” and invoking them when we need something or when we want to notify someone. This can result in fewer lines of code but can complicate things if we overuse it. We would also need to be careful not to create retain cycles with this approach.&lt;/p&gt;

&lt;p&gt;I hope that this article helped you to understand or to strengthen your knowledge with this pattern.&lt;/p&gt;

</description>
      <category>ios</category>
      <category>swift</category>
      <category>architecture</category>
    </item>
  </channel>
</rss>
