iOS:调用 AWSMobileClient initialize() 使 getUserAttributes() 不调用回调

Posted

技术标签:

【中文标题】iOS:调用 AWSMobileClient initialize() 使 getUserAttributes() 不调用回调【英文标题】:iOS: Calling AWSMobileClient initialize() makes getUserAttributes() not invoking callback 【发布时间】:2020-11-22 08:17:57 【问题描述】:

这是一个代码 sn-p 我试图让它工作但到目前为止没有成功。 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()
        
    

【问题讨论】:

【参考方案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")
                
            
        

【讨论】:

谢谢。改用 Amplify,如果我回去会试试这个。

以上是关于iOS:调用 AWSMobileClient initialize() 使 getUserAttributes() 不调用回调的主要内容,如果未能解决你的问题,请参考以下文章

AWSAppSync 和 AWSMobileClient 不兼容

iOS AWS Appsync Fetch 回调从未被调用

AWSMobileClient 初始化在卸载并安装应用程序后返回用户状态“SignedIn”

方法应为虚拟类型

AWS Amplify Swift API 登录 iOS 移动应用程序

Google Sign In 调用 App Delegate 中的方法后关闭 View Controller (iOS / Swift)