How to use actor properties from the main actor without async/await

Use static properties to allow your app to access or reference an actor's property without async/await.

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 11/01/2023 at 09:29
Last Updated on 11/01/2023 at 09:52
<p>Two flowers that represent Swift and Xcode side by side.Beneath them sits the text "Reference Actor Properties from the Main Actor."</p>

Use static properties to allow your app to access or reference an actor's property without async/await.

SubscribeDownload Open Source Starter Project
<p>A screenshot of Xcode showing how global actor properties cannot be referenced outside of a task and require async/await within a task.</p>

If you attempt to access the property of an actor that is not a main actor in a UIView, outside a Task or within a Task without using await Xcode will alert you of one of the following errors:

  • Expression is 'async' but is not marked with 'await'.
  • Actor-isolated property 'counter' can not be referenced from the main actor.

This can become a problem when trying to update UI without waiting for the property to return its value and would ideally happen on the main thread whilst maintaining the thread-safe, concurrent behavior of the actors.

<p>A screenshot of Xcode showing how static global actor properties can be accessed without async/await and outside of a task.</p>

This can be resolved by making the actor's property static, enabling it to be accessed or used on the main thread as if it were a normal property whilst keeping the remaining actor properties and functionality outside of the main thread.

Looking to learn more about things you can do with Swift and XCode ?

Search our blog to find educational content on learning how to use Swift and XCode.

Search our Blog

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
SubscribeContact UsVisit our BlogView our ServicesView our Work

Partner with us

We would love to get to know you and see how we can help your organization with its goals and needs.
Let's Talk

Stay Informed

Get occasional updates about our company, research, and product launches.
Subscribe