UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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.

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.