Почему AWSCognitoCredentialsProvider.getIdentityId (). ContinueWith (block :) не запускает свой блок? - PullRequest
1 голос
/ 28 мая 2019

Я пытаюсь получить Идентификационный идентификатор для AWSCognitoCredentialsProvider, но блок не запускается.

Почему он не запускается и что я должен проверить, чтобы исследоватьЭта проблема?Или в чем решение?

    override func viewDidLoad() {
        super.viewDidLoad()

        //setup service config
        let serviceConfiguration: AWSServiceConfiguration = AWSServiceConfiguration(region: .USEast1, credentialsProvider: nil)

        //create a pool
        let configuration: AWSCognitoIdentityUserPoolConfiguration = AWSCognitoIdentityUserPoolConfiguration(clientId: "***", clientSecret: "***", poolId: "us-east-1_***")

        AWSCognitoIdentityUserPool.register(with: serviceConfiguration, userPoolConfiguration: configuration, forKey: "***")
        pool = AWSCognitoIdentityUserPool(forKey: "***")

        pool.delegate = self

        self.user = self.pool.currentUser()
        print("user=", self.user as Any)
        print("user.isSignedIn=", user.isSignedIn)

        let credentialsProvider: AWSCognitoCredentialsProvider = AWSCognitoCredentialsProvider(regionType: .USEast1, identityPoolId: "us-east-1:***", identityProviderManager: self.pool)
        let defaultServiceConfiguration: AWSServiceConfiguration = AWSServiceConfiguration(region: .USEast1, credentialsProvider: credentialsProvider)
        AWSServiceManager.default()!.defaultServiceConfiguration = defaultServiceConfiguration

        let taskNSString: AWSTask<NSString> = credentialsProvider.getIdentityId()
        print("taskNSString.result=", taskNSString.result as Any)

        // Retrieve your Amazon Cognito ID
        credentialsProvider.getIdentityId().continueWith(block: {
            (task) -> AnyObject? in
            if (task.error != nil) {
                print("Error: " + task.error!.localizedDescription)
            }
            else {
                // the task result will contain the identity id
                let cognitoId = task.result!
                print("Cognito id: \(cognitoId)")
            }
            return task;
        })

    }

    extension ViewController: AWSCognitoIdentityPasswordAuthentication {

        public func getDetails(_ authenticationInput: AWSCognitoIdentityPasswordAuthenticationInput, passwordAuthenticationCompletionSource: AWSTaskCompletionSource<AWSCognitoIdentityPasswordAuthenticationDetails>) {
            print("getDetails(_:authenticationInput:passwordAuthenticationCompletionSource:)")
            self.passwordAuthenticationCompletion = passwordAuthenticationCompletionSource
            DispatchQueue.main.async {
                if (self.userNameText == nil) {
                    self.userNameText = authenticationInput.lastKnownUsername
                    print("userNameText=", self.userNameText as Any)
                }

            }
        }

        func didCompleteStepWithError(_ error: Error?) {

            print("didCompleteStepWithError\n", error as Any)

        }


    }

    extension ViewController: AWSCognitoIdentityInteractiveAuthenticationDelegate {

        func startPasswordAuthentication() -> AWSCognitoIdentityPasswordAuthentication {
            print("startPasswordAuthentication")
            return self
        }

    }

Вот результаты печати в окне отладки:

user= Optional(<AWSCognitoIdentityUser: 0x600002644ab0>)
user.isSignedIn= false
startPasswordAuthentication
getDetails(_:authenticationInput:passwordAuthenticationCompletionSource:)
taskNSString.result= nil
startPasswordAuthentication
getDetails(_:authenticationInput:passwordAuthenticationCompletionSource:)
userNameText= Optional("***")

1 Ответ

0 голосов
/ 28 мая 2019

Поскольку вы возвращаете задачу (называемую Задачей A) внутри вашего continueWith:, блок не будет выполняться до тех пор, пока задача (Задача A) не будет завершена.

Документация AWSTask RETURN: A task that will be completed after block has run. If block returns a AWSTask, then the task returned from this method will not be completed until that task is completed.

TL: DR Попробуйте вернуть ноль в вашем continueWith:

...