aws-amplify / amplify-swift

A declarative library for application development using cloud services.
Apache License 2.0
442 stars 192 forks source link

Support for async/await (Swift Concurrency) #1626

Closed zpg6 closed 1 year ago

zpg6 commented 2 years ago

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

atierian commented 2 years ago

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

zpg6 commented 2 years ago

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

atierian commented 2 years ago

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.

zpg6 commented 2 years ago

@atierian Thanks for your reply, crossing my fingers!

zpg6 commented 2 years ago

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?

atierian commented 2 years ago

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.

patagoniacode commented 2 years ago

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

royjit commented 2 years ago

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.

patagoniacode commented 2 years ago

Thanks @royjit, and keep up the great work!

thisisabhash commented 1 year ago

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!