BLACK FRIDAY SALE: Save 50% on all my Swift books and bundles! >>

Day 77: Performing async function using .onChange

Forums > 100 Days of SwiftUI

Hello,

I have finished Day 77 Challenge (The project that users can add and store photo and name) and my code run without problem. However I can not make one of my functions async.

I use MVVM. This function is inside "AddPersonViewModel"

        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() })

I got "'async' call in a function that does not support concurrency" error. If I make function sync, then it runs but I get some purple errors during loading images.

   

Hacking with Swift is sponsored by Guardsquare

SPONSORED AppSweep by Guardsquare helps developers automate the mobile app security testing process with fast, free scans. By using AppSweep’s actionable recommendations, developers can improve the security posture of their apps in accordance with security standards like OWASP.

Learn more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.