0

这是我试图让它工作但到目前为止没有成功的代码片段。initialize() 工作正常,但 getUserAttributes() 没有触发回调。不只是 getUserAttributes(),甚至其他 AWS 调用(例如 getTokens())也没有触发。相信,在 AWS 代码内部的某些地方,它被阻塞了。如果我注释掉 initialize() 则调用 getUserAttributes() 回调。使用 DispatchQueue/DispatchGroup 尝试了各种选项,但没有任何帮助。

AWSMobileClient pod 版本 2.12.7。

import Foundation
import AWSMobileClient

struct AWSUser {
    static let serialQueue = DispatchQueue(label: "serialQueue")
    static let group = DispatchGroup()

    static func initialize() -> Void {
        DispatchQueue.global(qos: .background).async {
            AWSInitialize()
            getAWSUserAttributes()
        }
    }

    static func AWSInitialize() -> Void {

        group.enter()

        AWSMobileClient.default().initialize { (userState, error) in
            // error handling ...

            switch userState {
            case .signedIn:
                //getAWSUserAttributes()
                break
            default:
                break
            }
            group.leave()
        }
    }

    static func getAWSUserAttributes() {
        group.wait()
        group.enter()
        AWSMobileClient.default().getUserAttributes { (attrs, error) in
            // NEVER REACHED!!!
            // BUT WORKS IF AWSMobileClient.default().initialize() is commented out
            group.leave()
        }
    }
}
4

1 回答 1

1

对于获取回调或触发 AWSMobile 客户端的任何事件,请确保您已在 AppDelegate 或相应的视图控制器中实施以下代码。如果此方法实现,则函数被触发...

 //Initialised Use Pool
        func intializeUserPool() -> Void {
            
            AWSDDLog.sharedInstance.logLevel = .verbose // TODO: Disable or reduce log level in production
            AWSDDLog.add(AWSDDTTYLogger.sharedInstance) // TTY = Log everything to Xcode console
            
            //Important for event handler
            initializeAWSMobileClient()
            
        }
        
        // Add user state listener and initialize the AWSMobileClient
        func initializeAWSMobileClient() {
            
            AWSMobileClient.default().initialize { (userState, error) in
                print("Initialise userstate:\(String(describing: userState)) and Info:\(String(describing: error))")
                if let userState = userState {
                    switch(userState){
                    case .signedIn: // is Signed IN
                        print("Logged In")
                        print("Cognito Identity Id (authenticated): \(String(describing: AWSMobileClient.default().identityId))")
                    case .signedOut: // is Signed OUT
                        print("Logged Out")
                        print("Cognito Identity Id (unauthenticated): \(String(describing: AWSMobileClient.default().identityId))")
                    case .signedOutUserPoolsTokenInvalid: // User Pools refresh token INVALID
                        print("User Pools refresh token is invalid or expired.")
                    default:
                        self.signOut()
                    }
                } else if let error = error {
                    print(error.localizedDescription)
                }
            }
            
            //Register State
            self.addUserStateListener() // Register for user state changes
        }
        
        // AWSMobileClient - a realtime notifications for user state changes
        func addUserStateListener() {
            
            AWSMobileClient.default().addUserStateListener(self) { (userState, info) in
                print("Add useruserstate:\(userState) and Info:\(info)")
                switch (userState) {
                case .signedIn:
                    print("Listener status change: signedIn")
                    DispatchQueue.main.async {
                        self.getSession()
                    }
                case .signedOut:
                    print("Listener status change: signedOut")
                case .signedOutFederatedTokensInvalid:
                    print("Listener status change: signedOutFederatedTokensInvalid")
                default:
                    print("Listener: unsupported userstate")
                }
            }
        }
于 2020-09-01T13:54:56.320 回答