Reputation: 853
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)
}
})
}
})
}
Upvotes: 10
Views: 7711
Reputation: 4044
you should "pop" back to the main thread to do this. Its pretty simple, just wrap
self.dismissViewControllerAnimated(true, completion: nil)
like this...
Swift 2.x
dispatch_async(dispatch_get_main_queue()){
self.dismissViewControllerAnimated(true, completion: nil)
}
Swift 3 4 & 5:
DispatchQueue.main.async {
self.dismiss(animated: true, completion: nil)
}
Upvotes: 18