Error: "'async' call in an autoclosure that does not support concurrency"


  
    @StateObject private var vm = QuotesViewModelImpl(service: QuotesServiceImpl()) --> Error Here

    
    var body: some View {

        Group {

            if vm.quotes.isEmpty {

                LoadingView(text: "Fetching Quotes")

            } else {

                List {

                    ForEach(vm.quotes, id: \.anime) { quote in

                        QuoteView(quote: quote)

                    }

                }
            }

        }

        .task {

            await vm.getRandomQuotes()
        }

    }    

}

Here is the ViewModel:


final class QuotesViewModelImpl: QuotesViewModel {

    @Published private(set) var quotes: [Quote] = []

    private let service: QuotesService

    init(service: QuotesService) async {

        self.service = service

    }

    func getRandomQuotes() async {

        do {

            self.quotes = try await service.fetchRandomQuotes()

        } catch {
            print(error)
        }
    }
}

Your QuotesViewModelImpl.init is marked as being async, and this is causing the issue.


When you create your vm property, you are using the StateObject property wrapper which is initialised with this:

init(wrappedValue thunk: @autoclosure @escaping () -> ObjectType) // ObjectType ends up being your QuotesViewModelImpl


QuotesViewModelImpl needs to be initialised asynchronously, but in a place that doesn't support concurrency – no async – so you can't use that initialiser.

Since there is nothing in that initialiser that needs to be executed asynchronously (i.e. called with await), I suggest you remove the async.

Error: "'async' call in an autoclosure that does not support concurrency"
 
 
Q