{"id":45705,"date":"2024-11-07T13:58:30","date_gmt":"2024-11-07T18:58:30","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=45705"},"modified":"2024-11-07T20:51:05","modified_gmt":"2024-11-08T01:51:05","slug":"problematic-swift-concurrency-patterns","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2024\/11\/07\/problematic-swift-concurrency-patterns\/","title":{"rendered":"Problematic Swift Concurrency Patterns"},"content":{"rendered":"<p><a href=\"https:\/\/www.massicotte.org\/problematic-patterns\">Matt Massicotte<\/a>:<\/p>\n<blockquote cite=\"https:\/\/www.massicotte.org\/problematic-patterns\"><p>I think the vast majority of the time, a global actor should be applied to the type as a whole, not to individual properties.<\/p><p>[&#8230;]<\/p><p>But, while <code>detached<\/code> does prevent isolation inheritance, it <strong>also<\/strong> does other stuff too. Detached tasks do not inherit priority or task-local values. Instead, think about a <code>nonisolated<\/code> function.<\/p><p>[&#8230;]<\/p><p>I think <code>MainActor.run<\/code> is rarely the right solution.<\/p><p>[&#8230;]<\/p><p>Moving from completion handlers to async methods can change semantics and cause code to run on background threads. Be <em>really<\/em> careful here!<\/p><p>[&#8230;]<\/p><p>The compiler will automatically generate async versions of Objective-C completion handler-based methods. The bad news is, unless the type itself is <code>MainActor<\/code>-isolated or <code>Sendable<\/code>, these translations will be problematic. They won&rsquo;t be possible to use diagnostic-free without a <code>@preconcurrency import<\/code> and will have different semantics that could make them unsafe.<\/p><\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2024\/10\/01\/swift-concurrency-and-objective-c\/\">Swift Concurrency and Objective-C<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2024\/09\/20\/unwanted-swift-concurrency-checking\/\">Unwanted Swift Concurrency Checking<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2024\/09\/19\/swift-6\/\">Swift 6<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2023\/10\/02\/the-bleeding-edge-of-swift-concurrency\/\">The Bleeding Edge of Swift Concurrency<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Matt Massicotte: I think the vast majority of the time, a global actor should be applied to the type as a whole, not to individual properties.[&#8230;]But, while detached does prevent isolation inheritance, it also does other stuff too. Detached tasks do not inherit priority or task-local values. Instead, think about a nonisolated function.[&#8230;]I think MainActor.run [&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-11-07T18:58:33Z","apple_news_api_id":"2a365f13-c34c-4f98-a065-0edeedf7327e","apple_news_api_modified_at":"2024-11-08T01:51:08Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAA==","apple_news_api_share_url":"https:\/\/apple.news\/AKjZfE8NMT5igZQ7e7fcyfg","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":[31,2586,30,2598,71,2200,901],"class_list":["post-45705","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-ios","tag-ios-18","tag-mac","tag-macos-15-sequoia","tag-programming","tag-swift-concurrency","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/45705","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=45705"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/45705\/revisions"}],"predecessor-version":[{"id":45710,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/45705\/revisions\/45710"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=45705"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=45705"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=45705"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}