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 the main thread?
import SwiftUI
@MainActor
class ViewModel: ObservableObject {
@Published private(set) var questionnaire: Questionnaire!
init() {
fetchQuestionnaire(id: "forest")
}
func fetchQuestionnaire(id: String) {
Task {
questionnaire = Questionnaire.testData
}
}
}
Or, is it actually OK to mutate @Published
properties on a background thread, and SwiftUI will perform the didSet
publisher on the main thread?
And if that’s the case, what even really is the point of @MainActor
in this situation?