In SwiftUI, I am trying to call an async function(which is for loading images that is picked by ImagePicker) by using .onChange. I got "'async' call in a function that does not support concurrency" error. If I make function sync and remove "Task {@MainActor in" section, then it runs but I get some purple errors during loading images which I do not want.
Is it possible to use .onChange to call async function?
func loadImage() async {
Task { @MainActor in
guard let inputImage = inputImage else {
loadingState = .failed
return
}
jpegData = inputImage.jpegData(compressionQuality: 0.8)!
image = Image(uiImage: inputImage)
loadingState = .loaded
}
}
I try to call the function as below;
.onChange (of: addPersonViewModel.inputImage, perform: { _ in addPersonViewModel.loadImage() })
If I make function sync and remove "Task {@MainActor in" section, then it runs but I get some purple errors during loading images which I do not want.
struct ContentViewthat can be pasted into Xcode. Please read minimal reproducible example