<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Emircan Aydın on Medium]]></title>
        <description><![CDATA[Stories by Emircan Aydın on Medium]]></description>
        <link>https://medium.com/@emircanaydin?source=rss-bc9d01a6b974------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*7miEKh0StBiYnXlFPtrQIQ.jpeg</url>
            <title>Stories by Emircan Aydın on Medium</title>
            <link>https://medium.com/@emircanaydin?source=rss-bc9d01a6b974------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 21:03:12 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@emircanaydin/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[What is the advantages MainActor in SwiftUI ?]]></title>
            <link>https://medium.com/@emircanaydin/what-is-the-advantages-mainactor-in-swiftui-f5e1c08ab3f7?source=rss-bc9d01a6b974------2</link>
            <guid isPermaLink="false">https://medium.com/p/f5e1c08ab3f7</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[swiftui]]></category>
            <dc:creator><![CDATA[Emircan Aydın]]></dc:creator>
            <pubDate>Fri, 06 May 2022 21:50:27 GMT</pubDate>
            <atom:updated>2022-05-06T21:57:19.046Z</atom:updated>
            <content:encoded><![CDATA[<h3>What is the advantages MainActor in SwiftUI ?</h3><p>MainActor is a new attribute introduced in Swift 5.5 as a global actor providing an executor which performs its tasks on the main thread. Using the @MainActor attribute will help you make sure your UI is always updated on the main thread.</p><p>Let’s say we have two @Published properties on an observable object. Let’s mark the class with @MainActorto ensure these UI updates are in the main thread.</p><pre>@MainActor<br><strong>class</strong> BookViewModel: ObservableObject {<br>    @Published <strong>var</strong> bookName = &quot;The Trial&quot;<br>    @Published <strong>var</strong> numberOfPages = 92<br>}</pre><p>Currently, when we use@ObservedObject or @StateObject in view the whole view will run in main thread. Then we shouldn’t need to use @MainActor? No, if the work fetching the data using async/await, the view will not be executed on the main thread. Even though @StateObject or @ObservableObject guarantees to run on the SwiftUI main thread, using @MainActorattribute to run worker threads on main thread is more safer and more precise. The benefit of @MainActor is that it forces all methods to run in the main thread. We don’t need to use <em>DispatchQueue.main.async()</em> everywhere. The compiler does this for us.</p><p>Be careful, although @MainActor run code on the main actor, completion handlers that you call inside the main actor will still be work in the background.</p><p>If you do need to spontaneously run some code on the main actor, you can do that by calling <strong>MainActor.run()</strong> and providing your work. This allows you to safely push work onto the main actor no matter where your code is currently running, like this:</p><pre><strong>func</strong> couldBeAnywhere() <strong>async</strong> {<br>    <strong>await</strong> MainActor.run {<br>        print(&quot;This is on the main actor.&quot;)<br>    }<br>}<br><br><strong>await</strong> couldBeAnywhere()</pre><p>You can send back nothing from <strong>run()</strong> if you want, or send back a value like this:</p><pre><strong>func</strong> couldBeAnywhere() <strong>async</strong> {<br>    <strong>let</strong> result = <strong>await</strong> MainActor.run { () -&gt; Int <strong>in</strong><br>        print(&quot;This is on the main actor.&quot;)<br>        <strong>return</strong> 42<br>    }</pre><pre>    print(result)<br>}</pre><pre><strong>await</strong> couldBeAnywhere()</pre><p>If code is running on the main actor, the code you call with <strong>MainActor.run()</strong>will execute immediately. It does not wait for the loop to finish, as with <strong>DispatchQueue.main.async()</strong>.</p><p>If you wanted the work to be sent off to the main actor <em>without</em> waiting for its result to come back, you can place it in a new task like this:</p><pre><strong>func</strong> couldBeAnywhere() {<br>    Task {<br>        <strong>await</strong> MainActor.run {<br>            print(&quot;This is on the main actor.&quot;)<br>        }<br>    }</pre><pre>    // more work you want to do<br>}</pre><pre>couldBeAnywhere()</pre><p>This is particularly helpful when you’re inside a synchronous context, so you need to push work to the main actor without using the <strong>await</strong> keyword.</p><p><strong>Important:</strong> If your function is already running on the main actor, using <strong>await MainActor.run()</strong> will run your code immediately without waiting for the next run loop, but using <strong>Task</strong> as shown above <em>will</em> wait for the next run loop.</p><p>You can see this in action in the following snippet:</p><pre>@MainActor <strong>class</strong> ViewModel: ObservableObject {<br>    <strong>func</strong> runTest() <strong>async</strong> {<br>        print(&quot;1&quot;)</pre><pre>        <strong>await</strong> MainActor.run {<br>            print(&quot;2&quot;)</pre><pre>            Task { @MainActor <strong>in</strong><br>                print(&quot;3&quot;)<br>            }</pre><pre>            print(&quot;4&quot;)<br>        }</pre><pre>        print(&quot;5&quot;)<br>    }<br>}</pre><p>That marks the whole type as using the main actor, so the call to <strong>MainActor.run()</strong> will run immediately when <strong>runTest()</strong> is called. However, the inner <strong>Task</strong> will <em>not</em> run immediately, so the code will print 1, 2, 4, 5, 3.</p><p>Although it’s possible to create your own global actors, I think we should probably avoid doing so until we’ve had sufficient chance to build apps using what we already have.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f5e1c08ab3f7" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>