{"id":37586,"date":"2022-11-08T17:01:41","date_gmt":"2022-11-08T22:01:41","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=37586"},"modified":"2022-11-09T13:09:04","modified_gmt":"2022-11-09T18:09:04","slug":"swift-concurrency-tips","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2022\/11\/08\/swift-concurrency-tips\/","title":{"rendered":"Swift Concurrency Tips"},"content":{"rendered":"<p><a href=\"https:\/\/wojciechkulik.pl\/ios\/swift-concurrency-things-they-dont-tell-you\">Wojciech Kulik<\/a> (<a href=\"https:\/\/news.ycombinator.com\/item?id=33379414\">Hacker News<\/a>):<\/p>\n<blockquote cite=\"https:\/\/wojciechkulik.pl\/ios\/swift-concurrency-things-they-dont-tell-you\">\n<p>If you add <code>@MainActor<\/code> attribute to a function, it is not the same as wrapping the whole method in <code>DispatchQueue.main.async<\/code>. If your method contains <code>await<\/code> keyword, the code will be split into two pieces &#x2013; one before <code>await<\/code> and one after <code>await<\/code>, and as mentioned before, once the method hits <code>await<\/code> another pending call can start running even before the first one is finished.<\/p>\n<p>[&#8230;]<\/p>\n<p>You should remember to always mark a code with <code>@MainActor<\/code> if you require the Main Thread. You shouldn&rsquo;t make implicit assumptions. The problem is when you suddenly start adding <code>async-await<\/code> to an existing project that was implemented without using Swift Concurrency. You can easily get into trouble if you don&rsquo;t pay enough attention when mixing old code with <code>async-await<\/code>.<\/p>\n<p>[&#8230;]<\/p>\n<p>First of all, you should avoid long-running synchronous work in <code>Task<\/code>. For that, you should use GCD and custom queues. This way you will prevent blocking unintentionally some <code>TaskPriority<\/code>. However, you should be also aware that having too many custom queues is not recommended because of possible <a href=\"https:\/\/www.swiftpal.io\/articles\/how-to-avoid-thread-explosion-and-deadlock-with-gcd-in-swift\">Thread Explosion<\/a>. Therefore, you may try using <code>OperationQueue<\/code> to limit concurrency.<\/p>\n<p>[&#8230;]<\/p>\n<p>[If] you test your code on a simulator each <code>TaskPriority<\/code> will be limited to only 1 task running at the same time.<\/p>\n<\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2022\/05\/12\/mainactor-not-guaranteed\/\">@MainActor Not Guaranteed<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2021\/03\/16\/underused-and-overused-gcd-patterns\/\">Underused and Overused GCD Patterns<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2020\/11\/25\/libdispatchs-unmet-promise\/\">libdispatch&rsquo;s Unmet Promise<\/a><\/li>\n<\/ul>\n\n<p id=\"swift-concurrency-tips-update-2022-11-09\">Update (2022-11-09): <a href=\"https:\/\/twitter.com\/ConfusedVorlon\/status\/1590327784349786112\">Rob Jonson<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/ConfusedVorlon\/status\/1590327784349786112\"><p>Another misleading article saying \n@MainActor\n guarantees a function runs on the main thread.\nIt doesn&rsquo;t.\nIt only guarantees that code <em>you<\/em> compile will call on the main thread.\nSystem callbacks can still call this function on any thread.\nApple should document\/emphasise this.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Wojciech Kulik (Hacker News): If you add @MainActor attribute to a function, it is not the same as wrapping the whole method in DispatchQueue.main.async. If your method contains await keyword, the code will be split into two pieces &#x2013; one before await and one after await, and as mentioned before, once the method hits await [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2022-11-08T22:01:44Z","apple_news_api_id":"58b97a54-a12e-4a32-81d9-33d53f650fce","apple_news_api_modified_at":"2022-11-09T18:09:07Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAQ==","apple_news_api_share_url":"https:\/\/apple.news\/AWLl6VKEuSjKB2TPVP2UPzg","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":[800,31,2185,30,2223,71,2200,901],"class_list":["post-37586","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-concurrency","tag-ios","tag-ios-16","tag-mac","tag-macos-13-ventura","tag-programming","tag-swift-concurrency","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/37586","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=37586"}],"version-history":[{"count":3,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/37586\/revisions"}],"predecessor-version":[{"id":37604,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/37586\/revisions\/37604"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=37586"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=37586"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=37586"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}