Initialize app with an Async function | SwiftUI

I need my app to configure the backend at start, here's the function to do so:

// Initializes Amplify
final func configureAmplify() async {
    do {
//            Amplify.Logging.logLevel = .info
        let dataStore = AWSDataStorePlugin(modelRegistration: AmplifyModels())
        let syncWithCloud = AWSAPIPlugin()
        let userAuth = AWSCognitoAuthPlugin()

        try Amplify.add(plugin: userAuth)
        try Amplify.add(plugin: dataStore)
        try Amplify.add(plugin: syncWithCloud)
        try Amplify.configure()
        print("Amplify initialized")
    } catch {
        print("Failed to initialize Amplify with \(error)")
    }
}

I tried placing it in the @main init like so:

init() async {
    await networkController.configureAmplify()
}

but I get the following error:

Type 'MyApplicationNameApp' does not conform to protocol 'App'

I try to apply the suggestions after that which is to initialize it:

init() {

}

but it seems odd, so now I have 2 init. What is going on here and what is the correct way to initialize multiple async functions at the start of the app, example:

  1. Code above (configure amplify)
  2. Check if user is logged in
  3. Set session

etc

Note: The init() async never gets called in the example above which is another problem within this question, so what is the correct way to initialize async function when the app starts.

  • you could try using task, such as

    .task { await networkController.configureAmplify() }

Add a Comment