10

I'm trying to get the user sign in and I'm getting the following error:

Generic parameter 'T' could not be inferred

This is the code:

// Gets User signed-in
func getUser() async throws -> AuthUser {
    do {
        try await withUnsafeThrowingContinuation { continuation in
            if let user = Amplify.Auth.getCurrentUser() {
                continuation.resume(returning: user )
            }
        }
    } catch(let error) {
        print(error)
    }
}

Why is that?

Arturo
  • 3,254
  • 2
  • 22
  • 61
  • What line is getting the error? – Oscar Apeland Sep 19 '21 at 21:15
  • @OscarApeland on line 4, : `try await withUnsafeThrowingContinuation { continuation in` – Arturo Sep 19 '21 at 21:16
  • I've come across this type of errors in my code before, and although your code need some tuning as mentioned in the comments, it is when you call `getUser()` that the error probably appears. Could you try this: `let user: AuthUser = await getUser()` where you specify the return type `let user: AuthUser` This will help inferring the correct type, it usually works for me. – workingdog support Ukraine Sep 19 '21 at 23:15

1 Answers1

10

Actually my call was not good at all in the first place, this is how it should be done:

    // Gets User signed-in
    func getUser() async throws -> AuthUser {
        return try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<AuthUser, Error>) in
            if let user = Amplify.Auth.getCurrentUser() {
                continuation.resume(returning: user)
                return
            } else {
                signOut()
                continuation.resume(with: .failure(YourCustomEnumError_or_the_actual_error))
                return
            }
        }
    }

For more info go here

Arturo
  • 3,254
  • 2
  • 22
  • 61
  • 2
    Specify a return type on the `continuation in`, like `(continuation: CheckedContinuation) in`... – Oscar Apeland Sep 19 '21 at 21:27
  • idk why it's not catching that from inferred, maybe cus of the `if` without an `else` – Oscar Apeland Sep 19 '21 at 21:28
  • I added an option in the else, so if the user is nil it would logout and go to the initial page, isn't it the same? @OscarApeland – Arturo Sep 19 '21 at 21:29
  • `nil` doesn't have a type, but again idk. maybe its a bug. either way specifying your return type should fix things – Oscar Apeland Sep 19 '21 at 21:30
  • The way I have it on the answer now it works, are you saying it's incorrect? @OscarApeland – Arturo Sep 19 '21 at 21:31
  • The `T` your error is talking about is the `YourUserModel` i mentioned – Oscar Apeland Sep 19 '21 at 21:31
  • Ohh I see thanks @OscarApeland – Arturo Sep 19 '21 at 21:32
  • @Arturo I think you're just running up against the limits of the type inference algorithm. I'm not sure why adding an `else` clause would help, I'm surprised it did. – Alexander Sep 19 '21 at 21:32
  • @OscarApeland "`nil` doesn't have a type" It most certainly does. `nil` is just a shorthand for `Optional.none`, for some statically known type. The type of `nil` is always `Optional`. – Alexander Sep 19 '21 at 21:33
  • @Alexander My thinking was that it would look for a type that is returned in all patterns. But `nil` should be inferred to `T?.none` so that is weird. I don't think his `else` helped out. – Oscar Apeland Sep 19 '21 at 21:33
  • @Alexander it didn't help, with the if was enough, that else is to transition the user to the login screen for safety but without it works too – Arturo Sep 19 '21 at 21:33
  • @Alexander oh man... I got to fix a couple of things then, thanks for the heads up. – Arturo Sep 19 '21 at 21:37
  • 1
    @Alexander lol so glad I asked this question – Arturo Sep 19 '21 at 21:40
  • Shouldn't the continuation be called on the else branch as well with an error? You code will crash when it gets on the else branch as the continuation has to always be called once. – Emil Oct 14 '22 at 11:27
  • Yes, it has to continue, I'll fix it @Emil – Arturo Oct 14 '22 at 20:23