Questions tagged [mainactor]
14 questions
14
votes
6 answers
asyncDetached falling back into main thread after MainActor call
I'm trying out the new async/await stuff. My goal here is to run the test() method in the background, so I use Task.detached; but during test() I need to make a call on the main thread, so I'm using MainActor.
(I realize that this may look…

matt
- 515,959
- 87
- 875
- 1,141
9
votes
1 answer
SwiftUI @MainActor loses global actor
I've got an ObservableObject class that I am calling to via @StateObject from my view. Inside that class is a function and the entire class is marked @MainActor. In my view there is a TextField with an onChange modifier that has a perform calling to…

kittonian
- 1,020
- 10
- 22
5
votes
2 answers
How to initialize Swift class annotated @MainActor for XCTest, SwiftUI Previews, etc
We'd like to make use of the @MainActor Annotation for our ViewModels in an existing SwiftUI project, so we can get rid of DispatchQueue.main.async and .receive(on: RunLoop.main).
@MainActor
class MyViewModel: ObservableObject {
private var…

NicolasBrunner
- 83
- 6
5
votes
1 answer
What is the best solution for background task using Swift async, await, @MainActor
I’m studying async, await, @MainActor of Swift.
I want to run a long process and display the progress.
import SwiftUI
@MainActor
final class ViewModel: ObservableObject {
@Published var count = 0
func countUpAsync() async {
…

Shuji Fukumoto
- 97
- 1
- 5
4
votes
1 answer
Using @MainActor in a Task which is calling async function declared as @MainActor
I would like to understand if it's needed to declare the Task itself as MainActor considering the following block of code.
func login() {
Task { [weak self] in
let result = await self?.loginService.start()
if result ==…

Zagahr
- 43
- 4
3
votes
1 answer
Are extensions of @MainActor classes on the main actor?
If a class is on @MainActor:
@MainActor class MyClass : NSObject
{
}
does this put all its extensions on @MainActor as well?
extension MyClass
{
}

meaning-matters
- 21,929
- 10
- 82
- 142
1
vote
1 answer
@MainActor isn't forcing compiler check when mutating @Observable property on background thread
Why does the below code not create a compiler error?
I am mutating a @Published property of this ObservableObject on a background thread. Shouldn't the @MainActor tag on this class mean that any code which mutates a published property must occur on…

Plato
- 111
- 2
- 7
1
vote
2 answers
SwiftUI Using @MainActor with EnvironmentKey
I Use @MainActor with view model class as shown in the code below, when I tried to add Environment Key for the model the following error appear:
"Call to main actor-isolated initializer 'init()' in a synchronous nonisolated context"
and code not…

Ammar Ahmad
- 534
- 5
- 24
0
votes
0 answers
How to initialise a custom WKWebView view without having to explicitly trigger the main thread (@MainActor)
I have a custom WKWebView that I want to utilise in my code:
public class AuthenticatedWebView: WKWebView, AdInAuthenticatedWebView {
private var shouldStopFurtherRedirects = false
public weak var authDelegate: AuthenticatedWebViewDelegate?
…

Saad Qureshi
- 728
- 2
- 7
- 21
0
votes
0 answers
how to punt one func off the main thread in a class derived from a @MainActor class
how would I do this:
@MainActor class Foo {}
class Bar: Foo {
func caller()
{
// how would I invoke somethingIneedToRunOnThreadOtherThanMain so that it runs on thread other than main?
}
func…

Anton Tropashko
- 5,486
- 5
- 41
- 66
0
votes
1 answer
How can i pass sortDescriptors into CloudKitUtility.fetch() as parameter in @MainActor environment?
Non-sendable type '[NSSortDescriptor]?' exiting main actor-isolated context in call to non-isolated static method 'fetch(recordType:predicate:sortDescriptions:resultsLimit:)' cannot cross actor boundary
Following code will report this error with…

foolbear
- 726
- 1
- 7
- 19
0
votes
0 answers
Sign in with Apple MainActor issues
Swift 5.6, iOS 15.5
I have a class that is responsible for handling Sign in with Apple.
class SignInWithApple: NSObject, ASAuthorizationControllerDelegate, ASAuthorizationControllerPresentationContextProviding {
private var view: UIView
…

Joris Mans
- 6,024
- 6
- 42
- 69
0
votes
2 answers
How to use URLSession in MainActor
I have a ViewModel that is isolated with @MainActor and I assume that every part of this ViewModel class should run on the main actor (main thread), right?
So the question is, what about the async call of URLSession.shared.data?
Does it also run on…

Okan Kocyigit
- 13,203
- 18
- 70
- 129
-1
votes
1 answer
I'm getting a purple warning when trying to save via @AppStorage inside an asynchronous call
In the code below I ask the server for the popuplation rate for the city the user is current in via HTTP request.
Everything works as expected except that I'm getting a purple warning when I save lastSearchedCity and lastSearchedPopulationRate to…

fs_tigre
- 10,650
- 13
- 73
- 146