Swift - dismissing view controller from within a completion block

Viewed 6787

Simple question. If I'm within a completion block - for example facebook login using firebase ... and the login has succeeded. If I want to dismiss the current view controller (login viewcontroller) from within the completion block , do I need to get back to main queue to do this. I'm assuming the login completion block is being done on a background thread and any change to UI (ie dismissing the current view controller) should be done on the main thread ... what's best practice here ... or am I missing something?

@IBAction func facebookLoginTapped(sender: AnyObject) {

    //
    let ref = Firebase(url: "https://XXXX.firebaseio.com")
    let facebookLogin = FBSDKLoginManager()
    facebookLogin.logInWithReadPermissions(["email"], fromViewController: self, handler: { (facebookResult, facebookError) -> Void in
        if facebookError != nil {
            print("Facebook login failed. Error \(facebookError)")
        } else if facebookResult.isCancelled {
            print("Facebook login was cancelled.")
        } else {
            //successfully logged in
            //get facbook access token
            let accessToken = FBSDKAccessToken.currentAccessToken().tokenString
            //use access token to authenticate with firebase
            ref.authWithOAuthProvider("facebook", token: accessToken,
                withCompletionBlock: { error, authData in
                    if error != nil {
                        print("Login failed. \(error)")
                    } else {
                        //authData contains
                        print("Logged in! \(authData)")

                        //pop loginvc back to uservc - DO I NEED TO GET MAIN THREAD HERE BEFORE DISMISSING VIEW CONTROLLER
                        self.dismissViewControllerAnimated(true, completion: nil)
                    }
            })
        }
    })
}
1 Answers
Related