AppIntent ignores registered dependencies when awaited

App intent has a perform method that is async and can throw an error, but I can't find a way to actually await the result and catch the error if needed.

If I convert this working but non-waiting, non-catching code:

Button("Go", intent: MyIntent())

to this (so I can control awaiting and error handling):

Button("Go") {
    Task { 
        do {
           try await MyIntent().perform() // 👈
        } catch {
           print(error)
        }
    }
}

It crashes:

AppDependency with key "foo" of type Bar.Type was not initialized prior to access. Dependency values can only be accessed inside of the intent perform flow and within types conforming to _SupportsAppDependencies unless the value of the dependency is manually set prior to access.

Although it is invalid since the first version is working like a charm and dependencies are registered in the @main App init method and it is in the perform flow.

So how can we await the result of the AppIntent and handle the errors if needed in the app? Should I re-invent the Dependency mechanism?

Answered by Frameworks Engineer in 868972022

Calling perform() directly is invalid, you need to use the callAsFunction() version which allows the dependencies to be injected

do {
  let intent = MyIntent()
  try await intent()
} catch {
  print(error)
}
Accepted Answer

Calling perform() directly is invalid, you need to use the callAsFunction() version which allows the dependencies to be injected

do {
  let intent = MyIntent()
  try await intent()
} catch {
  print(error)
}
AppIntent ignores registered dependencies when awaited
 
 
Q