I need to log in to Facebook with my app, fetch some data and AFTER this is finished, login to Firebase also. I am using dispatch queues for this but it is not working. My blocks don't wait for each other to finish at all.
Here is my code:
dispatch_queue_t loginQueue = dispatch_queue_create("com.balazsvincze.loginQueue", DISPATCH_QUEUE_SERIAL);
//Log in to Facebook
dispatch_async(loginQueue, ^{
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithReadPermissions: @[@"public_profile",@"user_friends"] fromViewController:viewController handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if(!error){
NSLog(@"Logged into Facebook");
self.facebookUserID = [[FBSDKAccessToken currentAccessToken] userID];
//Load my profile
[FBSDKProfile loadCurrentProfileWithCompletion:^(FBSDKProfile *profile, NSError *error){
if(!error){
self.name = profile.name;
}
else{
NSLog(@"Could not load profile: %@",error);
completionResult(NO);
return;
}
}];
}
else{
NSLog(@"Could not login: %@",error);
completionResult(NO);
return;
}
}];
});
//Log in to Firebase
dispatch_async(loginQueue, ^{
FIRAuthCredential *credential = [FIRFacebookAuthProvider credentialWithAccessToken:[FBSDKAccessToken currentAccessToken].tokenString];
[[FIRAuth auth] signInWithCredential:credential completion:^(FIRUser *user, NSError *error) {
if(!error){
NSLog(@"Logged into Firebase");
self.firebaseUserID = user.uid;
}
else{
NSLog(@"Could not login: %@",error);
completionResult(NO);
return;
}
}];
});
Any ideas? Thanks!