How to publish change from async function

Viewed 876

I have class conforms to ObservableObject with

@Published var fileContent = ""

defined. Further I have getFileContent() async function returning String. If I call function like this

Task {
    fileContent = await getFileContent(forMeasurementID: id, inContext: context)
}

code is compiled and app works fine but XCode is complaining "purple" error "Publishing changes from background threads is not allowed; make sure to publish values from the main thread (via operators like receive(on:)) on model updates.". I've tried to elaborate with receive(on:) but no succeess so far. I will appreciate any hint. Thanks.

1 Answers

You cannot change the state of your app (change a @Published var) unless you are in the main thread.

Here is how your code works:

Task {
    let content = await getFileContent(forMeasurementID: id, inContext: context)
    DispatchQueue.main.async {
        fileContent = content
    }
}
Related