swiftactorswift-concurrency

Swift, actor: Actor-isolated property 'scanning' can not be mutated from a non-isolated context


I have an actor:

actor StatesActor {

    var job1sActive:Bool = false
    ...

}

I have an object that uses that actor:

class MyObj {
    
    let myStates = StatesActor()
    
    func job1() async {
    
        myStates.job1IsActive = true

    }
}

Line:

myStates.job1IsActive = true

errors out with this error:

Actor-isolated property 'job1IsActive' can not be mutated from a non-isolated context

How can I use an actor to store/read state information correctly so the MyObj can use it to read and set state?


Solution

  • How can I use an actor to store/read state information correctly so the MyObj can use it to read and set state?

    You cannot mutate an actor's instance variables from outside the actor. That is the whole point of actors!

    Instead, give the actor a method that sets its own instance variable. You will then be able to call that method (with await).