{"id":44662,"date":"2024-08-29T15:18:48","date_gmt":"2024-08-29T19:18:48","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=44662"},"modified":"2024-08-29T15:18:48","modified_gmt":"2024-08-29T19:18:48","slug":"calling-async-code-synchronously-in-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2024\/08\/29\/calling-async-code-synchronously-in-swift\/","title":{"rendered":"Calling async Code Synchronously in Swift"},"content":{"rendered":"<p><a href=\"https:\/\/wadetregaskis.com\/calling-swift-concurrency-async-code-synchronously-in-swift\/\">Wade Tregaskis<\/a>:<\/p>\n<blockquote cite=\"https:\/\/wadetregaskis.com\/calling-swift-concurrency-async-code-synchronously-in-swift\/\"><p>I find my hand is often forced by APIs I don&rsquo;t control (most often Apple&rsquo;s APIs). e.g. data source or delegate callbacks that are synchronous and require you to return a value, but in order to obtain that value you have to run async code (perhaps because yet again that&rsquo;s all you&rsquo;re given by 3rd parties, or because that code makes sense to be async and is used happily as such in other places and you don&rsquo;t want to have to duplicate it in perpetuity just to have a sync version).<\/p><p>If that asynchronosity is achieved through e.g. <a href=\"https:\/\/developer.apple.com\/documentation\/DISPATCH\">GCD<\/a> or <a href=\"https:\/\/developer.apple.com\/documentation\/foundation\/nsrunloop\">NSRunLoop<\/a> or <a href=\"https:\/\/developer.apple.com\/documentation\/foundation\/process\">NSProcess<\/a> or <a href=\"https:\/\/developer.apple.com\/documentation\/foundation\/nstask\">NSTask<\/a> or <a href=\"https:\/\/developer.apple.com\/documentation\/foundation\/nsthread\">NSThread<\/a> or <a href=\"https:\/\/developer.apple.com\/library\/archive\/documentation\/System\/Conceptual\/ManPages_iPhoneOS\/man3\/pthread.3.html\">pthreads<\/a>, it&rsquo;s easy.  There are numerous ways to synchronously wait on their tasks.  In contrast, <a href=\"https:\/\/docs.swift.org\/swift-book\/documentation\/the-swift-programming-language\/concurrency\/\">Swift Concurrency<\/a> <em>really<\/em> doesn&rsquo;t want you to do this.  The language and standard library take an adamant idealogical position on this &#x2013; one which is unfortunately impractical; a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Spherical_cow\">spherical chicken in a vacuum<\/a>.<\/p><p>Nonetheless, despite Swift&rsquo;s best efforts to prevent me, I believe I&rsquo;ve come up with a way to do this.<\/p>\n<p>[&#8230;]<\/p>\n<p>Do not call this from a thread used by Swift Concurrency (e.g. an actor, including global actors like <code>MainActor<\/code>) if the closure - or anything it calls transitively via <code>await<\/code> - might be bound to that same isolation context.  Doing so may result in deadlock.<\/p><\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2024\/07\/29\/actor-reentrancy-in-swift\/\">Actor Reentrancy in Swift<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2024\/07\/22\/swift-6-announced\/\">Swift 6 Announced<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Wade Tregaskis: I find my hand is often forced by APIs I don&rsquo;t control (most often Apple&rsquo;s APIs). e.g. data source or delegate callbacks that are synchronous and require you to return a value, but in order to obtain that value you have to run async code (perhaps because yet again that&rsquo;s all you&rsquo;re given [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2024-08-29T19:18:51Z","apple_news_api_id":"f3c1ce29-da47-44c1-80ad-731298403039","apple_news_api_modified_at":"2024-08-29T19:18:51Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/A88HOKdpHRMGArXMSmEAwOQ","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[71,2200,901],"class_list":["post-44662","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-programming","tag-swift-concurrency","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/44662","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=44662"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/44662\/revisions"}],"predecessor-version":[{"id":44663,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/44662\/revisions\/44663"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=44662"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=44662"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=44662"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}