{"id":55896,"date":"2022-12-12T18:53:36","date_gmt":"2022-12-12T13:23:36","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=55896"},"modified":"2022-12-12T19:07:40","modified_gmt":"2022-12-12T13:37:40","slug":"async-await-modern-concurrency-in-swift","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/async-await-modern-concurrency-in-swift\/","title":{"rendered":"Async\/ Await (Modern Concurrency in Swift)"},"content":{"rendered":"<h2><b>Async\/ Await in Swift (Modern Concurrency)<\/b><\/h2>\n<h3>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<b>Async\/ Await<\/b><\/h3>\n<p><b>What is async\/Await?<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Apple introduces a new concurrency feature i.e., asynchronous (async) from Swift 5.5.<\/span><\/p>\n<p><b>Async<\/b><span style=\"font-weight: 400;\">:- stands for asynchronous, which means to execute the methods\/program asynchronously.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0<\/span><b>Await<\/b><span style=\"font-weight: 400;\">:- is the keyword to be used for calling async methods. Await is awaiting the result. It indicates that your program might pause its execution while it waits for the result.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0<\/span><b>Platform Supported<\/b><span style=\"font-weight: 400;\"> : <\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><b>Swift Version<\/b><span style=\"font-weight: 400;\">:- Swift 5.5\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>iOS Version<\/b><span style=\"font-weight: 400;\">: &#8211; iOS 15 onwards<\/span>\n<ul>\n<li style=\"font-weight: 400;\"><b>Backward compatibility<\/b><span style=\"font-weight: 400;\">: &#8211; iOS 13, Xcode 13.2, macOS 10.15, watchOS 6, and tvOS 13. This backward compatibility applies only to Swift language features; you can not use any APIs built using those language features, like the new <\/span><b>URLSession<\/b><span style=\"font-weight: 400;\"> APIs that use async\/await(for that, you still require iOS 15.0).\u00a0<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><b>How to create and call an asynchronous function?<\/b><\/p>\n<p><span style=\"font-weight: 400;\">The below function fetched the thumbnail and returned UIImage and String in a tuple. In this new type of asynchronous (async) function, we just add an <\/span><b>async<\/b><span style=\"font-weight: 400;\"> keyword before the return type. After that create a URL request, fetch the data from that request and create a thumbnail.<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignleft size-full wp-image-56007\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/1.png\" alt=\"\" width=\"2016\" height=\"356\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/1.png 2016w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/1-300x53.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/1-1024x181.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/1-768x136.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/1-1536x271.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/1-624x110.png 624w\" sizes=\"(max-width: 2016px) 100vw, 2016px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignleft wp-image-56008\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/2.png\" alt=\"\" width=\"487\" height=\"98\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/2.png 914w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/2-300x60.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/2-768x155.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/2-624x126.png 624w\" sizes=\"(max-width: 487px) 100vw, 487px\" \/><\/a><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignleft size-full wp-image-56009\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/3.png\" alt=\"\" width=\"2206\" height=\"122\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/3.png 2206w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/3-300x17.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/3-1024x57.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/3-768x42.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/3-1536x85.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/3-2048x113.png 2048w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/3-624x35.png 624w\" sizes=\"(max-width: 2206px) 100vw, 2206px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Here is the extension of UIImage to generate a thumbnail. byPreparingThumbnail is an asynchronous method which is why it marks as await.<br \/>\n<\/span><img decoding=\"async\" loading=\"lazy\" class=\"alignleft size-full wp-image-56011\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/4.png\" alt=\"\" width=\"1962\" height=\"304\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/4.png 1962w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/4-300x46.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/4-1024x159.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/4-768x119.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/4-1536x238.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/4-624x97.png 624w\" sizes=\"(max-width: 1962px) 100vw, 1962px\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400;\">This method is marked as an <\/span><b>async throw<\/b><span style=\"font-weight: 400;\"> which means it executes asynchronously and throws an error, if any.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">When calling an asynchronous function, We have to use await in front of the call to mark possible suspension points. When we request to download an image, asynchronous code suspends and frees the thread and handover that thread to the system to perform some other important\/Concurrent Task. And when it resumes later, it executes further on that same thread.\u00a0 <\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignleft size-full wp-image-56013\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/5.png\" alt=\"\" width=\"1744\" height=\"124\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/5.png 1744w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/5-300x21.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/5-1024x73.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/5-768x55.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/5-1536x109.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/5-624x44.png 624w\" sizes=\"(max-width: 1744px) 100vw, 1744px\" \/><\/a><\/p>\n<p><b>Call async function serially\u00a0 :<\/b><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0In the above code, all images are downloaded serially. We tell applications that wait for the first image to be returned until it can continue to fetch a second image, and so on. All images are in download in sequence. After that, We create an array of all downloaded images.<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignleft size-full wp-image-56014\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/6-1.png\" alt=\"\" width=\"1472\" height=\"294\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/6-1.png 1472w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/6-1-300x60.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/6-1-1024x205.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/6-1-768x153.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/6-1-624x125.png 624w\" sizes=\"(max-width: 1472px) 100vw, 1472px\" \/><\/a><\/p>\n<p><b>Async let: <\/b>call async function parallel.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignleft size-full wp-image-56015\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/7.1.png\" alt=\"\" width=\"1402\" height=\"316\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/7.1.png 1402w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/7.1-300x68.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/7.1-1024x231.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/7.1-768x173.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/7.1-624x141.png 624w\" sizes=\"(max-width: 1402px) 100vw, 1402px\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400;\">When we want to download images parallel, they have no dependent operation. We just need to put an async keyword before the let. <\/span><span style=\"font-weight: 400;\">Our array of images now needs to be defined using the <\/span><b>await<\/b><span style=\"font-weight: 400;\"> keyword as we\u2019re dealing with asynchronous constants.<\/span><\/p>\n<p><b>Drawback of completion handler approach:\u00a0<\/b><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignleft size-full wp-image-56016\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/8.0.png\" alt=\"\" width=\"1992\" height=\"854\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/8.0.png 1992w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/8.0-300x129.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/8.0-1024x439.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/8.0-768x329.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/8.0-1536x659.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/8.0-624x268.png 624w\" sizes=\"(max-width: 1992px) 100vw, 1992px\" \/><\/a><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">It\u2019s possible for the functions to call their completion handler more than once, or might possibly forget to call it entirely.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">In the above code, there are five possibilities for subtle bugs if we are unaware or forgot the completion handler\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">The parameter syntax <\/span><b>@escaping (String) -&gt; Void<\/b><span style=\"font-weight: 400;\"> can be hard to read.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">It was hard to send back errors with the completion handler (Until swift 5.0 added the Result type Result&lt;value&gt;)<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Thread Explosion<\/b><span style=\"font-weight: 400;\">: The completion handler approach blocks the thread and frees the thread when the function returns the result. So when the system creates multiple threads, and then the system is overcommitted, <strong>thread<\/strong><\/span><b>\u00a0explosion<\/b><span style=\"font-weight: 400;\"> occurs.\u00a0<\/span><\/li>\n<\/ul>\n<p><b>Benefit of the async await approach :<\/b><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignleft size-full wp-image-56017\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/9.0.png\" alt=\"\" width=\"2016\" height=\"356\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/9.0.png 2016w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/9.0-300x53.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/9.0-1024x181.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/9.0-768x136.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/9.0-1536x271.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/9.0-624x110.png 624w\" sizes=\"(max-width: 2016px) 100vw, 2016px\" \/><\/a><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Avoid the Pyramid of Doom problem with nested closures<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Reduction of code<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Easier to read<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Safety. With async\/await, a result is guaranteed, while completion blocks might or might not be called.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">It doesn\u2019t block the thread. Async suspended the function call and freed up the thread for other important work (that is decided by the system). The completion handler approach blocks the thread and frees the thread when the function returns the result\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">No thread explosion\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Co-Operative thread pool<\/b><span style=\"font-weight: 400;\">: &#8211; Avoid thread explosion and excessive context switching. It is designed in such a way that it reuses the ideal thread instead of blocking them.<\/span><\/li>\n<\/ul>\n<p><b>\u00a0Here are a few important things to remember about async\/await.<\/b><\/p>\n<ol>\n<li><span style=\"font-weight: 400;\"> First, when you mark a function async, you\u2019re allowing it to suspend. And when a function suspends itself, it suspends its callers too. So its callers must be async as well.<\/span><\/li>\n<li><span style=\"font-weight: 400;\"> await means async function may suspend execution one or many time<\/span><\/li>\n<li><span style=\"font-weight: 400;\"> when an async function is used thread is not blocked<\/span><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h3 style=\"text-align: center;\"><strong>TASK<\/strong><\/h3>\n<p><b>Task:\u00a0 <\/b><span style=\"font-weight: 400;\">Task is a Unit of asynchronous work. Every async function\/method is executed in a Task.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">How to create and run a Task ?<br \/>\n<img decoding=\"async\" loading=\"lazy\" class=\"alignleft size-full wp-image-56018\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/10.0.png\" alt=\"\" width=\"2034\" height=\"276\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/10.0.png 2034w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/10.0-300x41.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/10.0-1024x139.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/10.0-768x104.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/10.0-1536x208.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/10.0-624x85.png 624w\" sizes=\"(max-width: 2034px) 100vw, 2034px\" \/><\/a><\/span><\/p>\n<p><span style=\"font-weight: 400;\">Performing async method inside a Task\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">As you see above, when we call <\/span><b>\u00a0let<\/b><span style=\"font-weight: 400;\"> firstImage = <\/span><b>try<\/b><span style=\"font-weight: 400;\">? <\/span><b>await<\/b><span style=\"font-weight: 400;\"> fetchThumbnail(for: \u201c23\u201d) we got an error from the compiler I.e. <\/span><b>\u201c\u2018async\u2019 call in a function that does not support concurrency \u201d<\/b><\/p>\n<p><span style=\"font-weight: 400;\">What does that mean?\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">From WWDC21 The Swift compiler tells us that we cannot call async functions in contexts that aren\u2019t themselves async. when you\u2019ve tried to call an async function from a synchronous environment, which is not allowed. Asynchronous functions must be able to suspend themselves and their callers, and synchronous functions don\u2019t know how to do that.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">So what is the solution for this error? The solution is a <\/span><b>Task<\/b><span style=\"font-weight: 400;\">!<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The async task packages the work in the closure and sends it to the system for immediate execution on the next available thread, like an async function on a global dispatch queue.<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignleft size-full wp-image-56019\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/11.png\" alt=\"\" width=\"1792\" height=\"394\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/11.png 1792w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/11-300x66.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/11-1024x225.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/11-768x169.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/11-1536x338.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/11-624x137.png 624w\" sizes=\"(max-width: 1792px) 100vw, 1792px\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400;\">Another way to solve this error is, We can mark the function async but using this; we will get an error in the upper hierarchy <\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignleft size-full wp-image-56020\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/12.png\" alt=\"\" width=\"1552\" height=\"376\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/12.png 1552w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/12-300x73.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/12-1024x248.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/12-768x186.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/12-1536x372.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/12-624x151.png 624w\" sizes=\"(max-width: 1552px) 100vw, 1552px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Like somewhere when we call fetchThumbnail() method, it gives the same error <\/span><b>\u201c\u2018async\u2019 call in a function that does not support concurrency.\u201d\u00a0 <\/b><span style=\"font-weight: 400;\">So to resolve this again we have to use the task<\/span><b>\u00a0<\/b><\/p>\n<p><b>Task has more feature like:\u00a0<\/b><\/p>\n<p><b>1. Task priority<\/b><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">We can decide task priority like background, utility etc, but priority can also be nil if no priority is assigned as we have done above.<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignleft size-full wp-image-56021\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/13.png\" alt=\"\" width=\"2016\" height=\"396\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/13.png 2016w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/13-300x59.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/13-1024x201.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/13-768x151.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/13-1536x302.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/13-624x123.png 624w\" sizes=\"(max-width: 2016px) 100vw, 2016px\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400;\">\u00a02. <\/span><b>Cancel<\/b><span style=\"font-weight: 400;\">: &#8211; Canceling task and its child task : canceling a parent task automatically cancels all of its child tasks.<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignleft size-full wp-image-56022\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/14.png\" alt=\"\" width=\"1076\" height=\"196\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/14.png 1076w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/14-300x55.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/14-1024x187.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/14-768x140.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/14-624x114.png 624w\" sizes=\"(max-width: 1076px) 100vw, 1076px\" \/><\/a><\/p>\n<p><b>3.Task.currentPriority: <\/b><span style=\"font-weight: 400;\">to check task priority<\/span><\/p>\n<p>&nbsp;<\/p>\n<h3 style=\"text-align: center;\">\u00a0 \u00a0 \u00a0Task Group<\/h3>\n<p><b>TaskGroup:\u00a0 <\/b><span style=\"font-weight: 400;\">TASK GROUP Help us to execute the concurrent and parallel tasks and group its result to return as the final (single) output\u00a0<\/span><\/p>\n<p><b>Creating a task: <\/b><span style=\"font-weight: 400;\">The task comes in two flavors.<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">By throwing an error\u00a0<\/span><\/li>\n<\/ol>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignleft size-full wp-image-56023\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/15.png\" alt=\"\" width=\"1466\" height=\"610\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/15.png 1466w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/15-300x125.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/15-1024x426.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/15-768x320.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/15-624x260.png 624w\" sizes=\"(max-width: 1466px) 100vw, 1466px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>2. Without throwing an error:<br \/>\n<img decoding=\"async\" loading=\"lazy\" class=\"alignleft size-full wp-image-56024\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/16.png\" alt=\"\" width=\"802\" height=\"636\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/16.png 802w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/16-300x238.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/16-768x609.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/16-624x495.png 624w\" sizes=\"(max-width: 802px) 100vw, 802px\" \/><\/a><img decoding=\"async\" loading=\"lazy\" class=\"alignleft size-full wp-image-56025\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/17.png\" alt=\"\" width=\"1962\" height=\"162\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/17.png 1962w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/17-300x25.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/17-1024x85.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/17-768x63.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/17-1536x127.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/17-624x52.png 624w\" sizes=\"(max-width: 1962px) 100vw, 1962px\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400;\">To create a task group, we can use <\/span><b>withTaskGroup<\/b><span style=\"font-weight: 400;\"> or <\/span><b>withThrowingTaskGroup<\/b><span style=\"font-weight: 400;\"> according to our needs. For now, we are going ahead withTaskGroup. So our code looks like below:\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">In this example, we will create a task group that has multiple child tasks that execute a UserOperation and return its result. When all operations end, the task group will collect all child task results and return them to a dictionary.<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignleft size-full wp-image-56026\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/18.png\" alt=\"\" width=\"2240\" height=\"730\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/18.png 2240w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/18-300x98.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/18-1024x334.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/18-768x250.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/18-1536x501.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/18-2048x667.png 2048w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/18-624x203.png 624w\" sizes=\"(max-width: 2240px) 100vw, 2240px\" \/><\/a><br \/>\n<span style=\"font-weight: 400;\">Let\u2019s say we have an array of Users, as shown below:<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignleft size-full wp-image-56027\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/19.png\" alt=\"\" width=\"1692\" height=\"322\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/19.png 1692w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/19-300x57.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/19-1024x195.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/19-768x146.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/19-1536x292.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/19-624x119.png 624w\" sizes=\"(max-width: 1692px) 100vw, 1692px\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400;\">We are returning a couple for the child task, and the task group returns a dictionary that contains the user&#8217;s full name and initials.<\/span><img decoding=\"async\" loading=\"lazy\" class=\"alignleft size-full wp-image-56028\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/20.png\" alt=\"\" width=\"2246\" height=\"1130\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/20.png 2246w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/20-300x151.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/20-1024x515.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/20-768x386.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/20-1536x773.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/20-2048x1030.png 2048w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/20-624x314.png 624w\" sizes=\"(max-width: 2246px) 100vw, 2246px\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400;\">As you can see, we have looped all the operations and added the child task to the group to process full name and initial. All child tasks run concurrently, and there is no control over when they finish, to collect the result of all child tasks, we have a loop task group. As we see above, the await keyword in looping indicates that the for-loop might suspend while waiting for a child task to complete. Whenever the child task returns the result, the for-loop iterates and updates the dictionary, and when all child is complete, then the for-in group exits and returns the group result.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">When we run this, we got the following result :<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignleft size-full wp-image-56029\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/21.png\" alt=\"\" width=\"2372\" height=\"326\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/21.png 2372w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/21-300x41.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/21-1024x141.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/21-768x106.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/21-1536x211.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/21-2048x281.png 2048w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/21-624x86.png 624w\" sizes=\"(max-width: 2372px) 100vw, 2372px\" \/><\/a><br \/>\n<span style=\"font-weight: 400;\">As you see, the result operation is completed in 10 sec. And all child tasks run concurrently and the task group returns only when all child tasks are completed. This also indicates that the child is only available in a task group context.<\/span><\/p>\n<h3 style=\"text-align: center;\"><b>Actor\u00a0<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">The <\/span><b>Actor<\/b><span style=\"font-weight: 400;\"> is used to protect the mutable state in swift. It helps us to protect data races.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Data Races occur when:\u00a0<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Two threads concurrently access the same data\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">And any of them modify the same data<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">Actor are similar to the class in most cases. Some points are discuss below:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Actor are reference type\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Actor have properties, methods, initializers, deinitializer, and subscripts<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Do not support inheritance.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Can execute one method at a time\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">It automatically confirms to Actor protocol.\u00a0<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">Here is some old thing that can replace by <\/span><b>Actor<\/b><span style=\"font-weight: 400;\">\u00a0 :<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">DispatchQueue.main.async<\/span><span style=\"font-weight: 400;\">\u00a0 use <\/span><b>MainActor<\/b><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">DispatchBarrier\/Lock <\/span><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0use <\/span><b>Actor<\/b> <span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Data Race <\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0use <\/span><b>Actor<\/b> <span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Let\u2019s jump to the old, How can we avoid data races? We used Dispatch Barrier or lock to save our program from DataRaces.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Here we create a small bank module in which we can deposit, withdraw and check the bank balance. In this example, we create a barrier queue to prevent data races while accessing mutable <\/span><b>availableBalance<\/b><span style=\"font-weight: 400;\"> variables. In all functions, we have used a barrier flag to allow access to one thread at a time.\u00a0<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignleft size-full wp-image-56030\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/22.png\" alt=\"\" width=\"1874\" height=\"1008\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/22.png 1874w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/22-300x161.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/22-1024x551.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/22-768x413.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/22-1536x826.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/22-624x336.png 624w\" sizes=\"(max-width: 1874px) 100vw, 1874px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Now we have created a queue to asynchronously withdraw and deposit amounts in our bank.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">As we run this, we can see the initial balance in the bank is 100, and when total balance after withdrawal and deposit is 90. This looks perfectly fine because this is an asynchronous operation. It&#8217;s possible to withdrawal before the deposit and vice versa.<\/span><img decoding=\"async\" loading=\"lazy\" class=\"alignleft size-full wp-image-56031\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/23.png\" alt=\"\" width=\"1328\" height=\"892\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/23.png 1328w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/23-300x202.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/23-1024x688.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/23-768x516.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/23-624x419.png 624w\" sizes=\"(max-width: 1328px) 100vw, 1328px\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400;\">Now let\u2019s do this again using <\/span><b>Actor<\/b><span style=\"font-weight: 400;\">. We have changed Class to Actor here and removed the barrier queue, which is used in class examples. An actor has serialized all its properties, ensuring that only one interaction happens at a single time, giving us complete protection against data races because all mutations are performed serially. So in the Bank actor, we have a mutable available balance, and it accesses serially; hence it protects against data races.\u00a0\u00a0<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignleft size-full wp-image-56032\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/24.png\" alt=\"\" width=\"1376\" height=\"704\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/24.png 1376w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/24-300x153.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/24-1024x524.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/24-768x393.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/24-624x319.png 624w\" sizes=\"(max-width: 1376px) 100vw, 1376px\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400;\">To support concurrency, we just mask await and bind this into the task, as explained in the async await and task section. <\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignleft size-full wp-image-56033\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/25.png\" alt=\"\" width=\"1678\" height=\"608\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/25.png 1678w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/25-300x109.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/25-1024x371.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/25-768x278.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/25-1536x557.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/25-624x226.png 624w\" sizes=\"(max-width: 1678px) 100vw, 1678px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><b>@MainActor: The main<\/b><span style=\"font-weight: 400;\">\u00a0actor is a globally unique actor which performs tasks on the main thread.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Earlier anything we need to update the UI in the main thread we have to use <\/span><b>dispatchQueue.main.async,<\/b><span style=\"font-weight: 400;\"> but using the <\/span><b>@mainActo<\/b><span style=\"font-weight: 400;\">r, we don\u2019t require this to update the UI in the main thread. In our bank example, there is a label on the UI on which we are going to show the balance. So to update UI on the main thread we have to mark showBalance function with\u00a0 <\/span><b>@mainActor <\/b><span style=\"font-weight: 400;\">attribute.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Main actor is a global actor that we can use with properties, methods, closures, and instances. For example, we have added an attribute <\/span><b>@mainActor<\/b><span style=\"font-weight: 400;\"> before the function declaration meaning that the function will be executed in the main thread. <\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignleft size-full wp-image-56034\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/26.png\" alt=\"\" width=\"2088\" height=\"1096\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/12\/26.png 2088w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/26-300x157.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/26-1024x538.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/26-768x403.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/26-1536x806.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/26-2048x1075.png 2048w, \/blog\/wp-ttn-blog\/uploads\/2022\/12\/26-624x328.png 624w\" sizes=\"(max-width: 2088px) 100vw, 2088px\" \/><\/a>Conclusion: That&#8217;s all for now. Thanks for reading.\u00a0 In Modern concurrency, there are more concepts remaining, like structure concurrency, actor isolation, actor hopping, etc but it&#8217;s not possible to cover all these sections, you can also check this out in WWDC21 and WWDC22.<\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>Async\/ Await in Swift (Modern Concurrency) \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0Async\/ Await What is async\/Await? Apple introduces a new concurrency feature i.e., asynchronous (async) from Swift 5.5. Async:- stands for asynchronous, which means [&hellip;]<\/p>\n","protected":false},"author":1518,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":70},"categories":[1400],"tags":[5058,5056,5055,5057],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/55896"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1518"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=55896"}],"version-history":[{"count":15,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/55896\/revisions"}],"predecessor-version":[{"id":56115,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/55896\/revisions\/56115"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=55896"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=55896"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=55896"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}