amplify-swift icon indicating copy to clipboard operation
amplify-swift copied to clipboard

Support for async/await (Swift Concurrency)

Open zpg6 opened this issue 3 years ago • 9 comments

Is your feature request related to a problem? Please describe.

Amplify is my go-to, and I think this feature would greatly increase DX for new-on-the-scene iOS devs looking for a modern cloud SDK.

Some background

The introduction of async/await has shaken many iOS devs, myself included. Hours spent debugging callbacks now can be avoided almost entirely. With fewer (or zero!) callbacks to maintain, state is now easier to drive, test, and collaborate on.

Why this package?

Amplify is not something iOS devs are using for one call. Most will use auth, a database, maybe other services. I imagine the need to manage several nested callbacks is almost universal.

Not for me to say if it is within scope of this package or would be better as a separate library. If not in scope, would maintainers be willing to support / advise?

Describe the solution you'd like

Without async/await

The following snippet is only half-accurate because there would be error handling and other factors involved. Yes, these nested callbacks can be handled in several ways, but in my opinion/experience just ridding your code of nested callbacks doesn't mean you're avoiding complexity.

// Sign them in...
Amplify.Auth.signIn(username: username, password: password) { result in
    switch result {
    case .success:
        // Now get their data...
        let request = RESTRequest(path: "/todos")
        Amplify.API.get(request: request) { result in
            switch result {
            case .success(let data):
                let str = String(decoding: data, as: UTF8.self)
                print("Todos: \(str)")
                // cross your fingers you don't have to fetch more...
            case .failure(let apiError):
                print("Failed", apiError)
            }
        }
    case .failure(let error):
        print("Sign in failed \(error)")
    }
}

With async/await

do {
  let user = try await Amplify.Auth.signIn(username: username, password: password)
  let request = RESTRequest(path: "/todos")
  let todos = try await Amplify.API.get(request: request)
  // [more requests if needed]
  // [handle fetched data]
} catch {
  switch error {
    // [handle errors]
  }
}

Describe alternatives you've considered

I'm invested in this feature, so I've nearly finished the content required to complete this pull request myself. I would be willing to contribute that as a starting point for a PR.

All I did was wrap the existing API methods I needed with:

return try await withCheckedThrowingContinuation { continuation in
  Amplify.someMethod() { result in
    continuation.resume(with: result)
  }
}

Probably not a good first issue on this repo, so I would likely need some assistance with all that this would require.

Is the feature request related to any of the existing Amplify categories?

Auth, API, Rekognition, Storage

Additional context

No response

zpg6 avatar Feb 09 '22 15:02 zpg6

Thanks for opening this very thorough feature request @zpg6! We'll comment here if we have any updates to share on Swift Concurrency support.

atierian avatar Feb 25 '22 18:02 atierian

@atierian Would it be worthwhile to submit design proposals in the issue here, or would this feature be handled by your team internally?

zpg6 avatar Mar 11 '22 13:03 zpg6

Thanks @zpg6! It’s great to see such enthusiasm around Swift structured concurrency support in Amplify; getting it right is something that’s very important to us. We’re currently in the early stages of exploring and designing what that could look like in Amplify. Once we have a more concrete design, we’ll come to the community with updates and to get feedback.

In the meantime, we strongly encourage anyone who would like to see structured concurrency in Amplify (async/await, actors, etc) to 👍 this feature request. The feedback we receive from the community helps us prioritize new features.

atierian avatar Mar 11 '22 20:03 atierian

@atierian Thanks for your reply, crossing my fingers!

zpg6 avatar Mar 11 '22 21:03 zpg6

Seeing Swift SDK Developer Preview Docs, this feature is mentioned briefly as prioritized on the roadmap.

@ maintainers should this issue be closed in favor of development on it in new sdk?

zpg6 avatar Apr 19 '22 16:04 zpg6

Hey @zpg6. We opened an RFC related to async / await support and would love your, and everyone else's, vote in the poll and/or any thoughts.

atierian avatar May 13 '22 22:05 atierian

Dear @atierian and Amplify team, any news about the async / await support? Thanks for all the work.

patagoniacode avatar Aug 25 '22 15:08 patagoniacode

Hi @patagoniacode, thank you for following up in this support. While we are still working on more improvements for Amplify iOS, we encourage you to try out our dev-preview branch and provide your feedback! We would really appreciate your feedback on our RFC on async/await support and please be assured that we will keep you updated when we have more concrete information on availability.

royjit avatar Aug 30 '22 15:08 royjit

Thanks @royjit, and keep up the great work!

patagoniacode avatar Aug 30 '22 19:08 patagoniacode

Hello everyone!

Today we launched Amplify Library for Swift (v2.0.0) with native macOS support (currently beta). This new version is built on top of the AWS SDK for Swift, making Amplify Library for Swift exclusively Swift based. Now the news you've been waiting for - It also fully embraces the new world of Swift Structured Concurrency, exposing async/await APIs for all categories.

You can read more about the release here and be sure to check out the documentation for v2.

We really appreciate your patience with this and can't wait for you to try it out!

thisisabhash avatar Oct 18 '22 21:10 thisisabhash