Async/Await and MainActor Strategies

<p>Pretty much every article that discusses Swift async/await will throw in a comment regarding&nbsp;<code>@MainActor</code>&nbsp;and how it can be used to ensure that any updates to the user interface will occur on the main thread.</p> <p>That&rsquo;s all well and good&hellip; but just where should the&nbsp;<code>@MainActor</code>&nbsp;attribute go? On the class? Only on the asynchronous function itself? Inside a nested Task block? On separate functions dedicated soley to updating the thread? Perhaps use something like&nbsp;<code>await MainActor.run</code>&nbsp;instead? Or should we just punt, use&nbsp;<code>DispatchQueue.main.async</code>, and forget the whole thing?</p> <p>Lots of people have opinions on the matter, and this article is no different in that regard.</p> <p>What is different, however, is that in this article we&rsquo;re going to look at some of the actual SIL (Swift Intermediate Language) code generated by those mechanisms to examine the relative efficiency and code size of each.</p> <p>And we&rsquo;ll also explore a few of the other ramifications involved.</p> <p>Ready? Coffee in hand? Let&rsquo;s get started.</p> <p><a href="https://betterprogramming.pub/async-await-and-mainactor-strategies-cc35b6c58b52">Website</a></p>
Tags: Async Swift Await