{"id":9220,"date":"2022-10-11T21:17:59","date_gmt":"2022-10-11T21:17:59","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=9220"},"modified":"2022-10-11T21:17:59","modified_gmt":"2022-10-11T21:17:59","slug":"swift-tasks","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/swift-tasks\/","title":{"rendered":"Swift Tasks"},"content":{"rendered":"<p>Swift uses the Task struct to execute code concurrently. We can run a Task and await the return value or we can use in a &#8220;fire and forget&#8221; manner. We create a task like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nTask {\r\n   \/\/ do something\r\n}\r\n<\/pre>\n<p>We do not need to call a <em>start<\/em> method or the likes, once created the code starts. If you intend to return a value you&#8217;d write something like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nlet result = await Task { () -&gt; String in\r\n   &quot;Hello World&quot;\r\n}.value\r\n<\/pre>\n<p>Ofcourse we might also have the possibility of throwing an exception, hence we&#8217;d use<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\ndo {\r\n   let result = try await Task { () -&gt; String in\r\n      throw MyError.failed\r\n   }.value\r\n} catch {\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Swift uses the Task struct to execute code concurrently. We can run a Task and await the return value or we can use in a &#8220;fire and forget&#8221; manner. We create a task like this Task { \/\/ do something } We do not need to call a start method or the likes, once created [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[286],"tags":[],"class_list":["post-9220","post","type-post","status-publish","format-standard","hentry","category-swift"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9220","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=9220"}],"version-history":[{"count":2,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9220\/revisions"}],"predecessor-version":[{"id":9222,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9220\/revisions\/9222"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=9220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=9220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=9220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}