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

New to me error: Cannot pass function of type '() async -> Void' to parameter expecting synchronous function type

Forums > SwiftUI

I need to call a function that reads and API. This code worked in a UIKit version of the app and i'm trying to migrate ot over to SwiftUI. I'm getting the error at the line:

                        Button(action: {
                        Button(action: {

                             print ("mob: Connect to API")

                             // Set the API connection parameters
                             let connectToken = $token
                             let connectUser = $user
                             let connectSlug = $slug
                             var APIResponse = ""

                             print("mob: Token: /(connectToken)")
                             print("mob: User: /(connectUser)")
                             print("mob: Slug: /(connectSlug)")

                             do {
                              try await APIResponse = (apiGetData(connectToken: connectToken!  , connectUser: connectUser!, connectSlug: connectSlug!))
                              } catch let error as NSError {
                                    print("mob: Failed with error: \(error.localizedDescription)")
                                }

I'm not sure if I should include the code for:

apiGetData

but what I can say is that that function is defined like this:

func apiGetData(connectToken: String, connectUser: String, connectSlug: String) async throws -> String {

I've been searching and playing with the code for some hours yesterday with no success so I thought i'd reach out to the experts.

Thanks for any advice.

1      

You can not run async method from a synchronous call. Try wrap the call in Task

Button. {
  Task {
// do stuff
  }
}

1      

Nigel's response is explained further in these sections of Paul's Swift Concurrency by Example book:

See the Button in the last example: https://www.hackingwithswift.com/quick-start/concurrency/what-calls-the-first-async-function

More explanation: https://www.hackingwithswift.com/quick-start/concurrency/how-to-create-and-run-a-task

1      

Functions that perform any kind of data manipualtion , save/edit can perform these actions either with local data or with data available on worldwideweb, the data that we fetch using internet connection, that reaches us in chunks , is performed in a very complex way, there are internet protocol, and other aspects involved in it .

So there are special words that we provide to compiler , so that it can perform these tasks and receive data, so when we try and perform these tasks with out giving enough information to compiler it informs us of that very lack of information ...

I would say that if one can understand "how things work", then what platform/framework we use , becomes less and less relevant, but its a long process ..

1      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.