iOS: Calling AWSMobileClient initialize() makes getUserAttributes() not invoking callback

912 views Asked by At

Here is a code snippet I am trying to get it work but without success so far. initialize() works fine but then getUserAttributes() is not triggering the callback. Not just getUserAttributes(), even other AWS calls such as getTokens() not triggering either. Believe, some where down inside AWS code, it is getting blocked. If I comment out initialize() then getUserAttributes() callback gets invoked. Tried various options with DispatchQueue/DispatchGroup, no help.

AWSMobileClient pod version 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

There are 1 answers

1
Renish Dadhaniya On

For Getting Callback or trigger any event of AWSMobile Client, Make sure you have implemented below code in AppDelegate or respective view controller. If this method implement then function is trigger...

 //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")
                }
            }
        }