Async/Await and MainActor Strategies
<p>Pretty much every article that discusses Swift async/await will throw in a comment regarding <code>@MainActor</code> and how it can be used to ensure that any updates to the user interface will occur on the main thread.</p>
<p>That’s all well and good… but just where should the <code>@MainActor</code> 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 <code>await MainActor.run</code> instead? Or should we just punt, use <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’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’ll also explore a few of the other ramifications involved.</p>
<p>Ready? Coffee in hand? Let’s get started.</p>
<p><a href="https://betterprogramming.pub/async-await-and-mainactor-strategies-cc35b6c58b52">Website</a></p>