BLACK FRIDAY: 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.

2      

Save 50% in my WWDC sale.

SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.