我需要使用我的应用登录Facebook,获取一些数据,完成后,还要登录Firebase。我正在使用调度队列,但它不起作用。我的积木不会等到对方完成。
这是我的代码:
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;
}
}];
});
有什么想法吗?谢谢!
答案 0 :(得分:2)
拨打电话,在Facebook的完成处理程序中登录Firebase,它将全部按预期工作
答案 1 :(得分:1)
虽然您要在串行调度队列上调度异步任务,但这些任务本身会调度异步任务然后结束;所以你的任务已经完成,但登录没有。
正如@Rob建议的那样,使用NSOperation
之类的代码将代码拆分为单独的任务将会产生更清晰,更具伸缩性的代码。
但是,以下是在异步操作完成时如何使用dispatch_group
和dispatch_group_notify
来安排某些代码:
dispatch_group_t dispatchGroup = dispatch_group_create();
//Log in to Facebook
dispatch_group_enter(dispatchGroup);
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
dispatch_group_enter(dispatchGroup);
[FBSDKProfile loadCurrentProfileWithCompletion:^(FBSDKProfile *profile, NSError *error){
if(!error){
self.name = profile.name;
}
else{
NSLog(@"Could not load profile: %@",error);
completionResult(NO);
}
dispatch_group_leave(dispatchGroup);
}];
}
else{
NSLog(@"Could not login: %@",error);
completionResult(NO);
}
dispatch_group_leave(dispatchGroup);
}];
//Log in to Firebase
dispatch_group_enter(dispatchGroup);
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);
}
dispatch_group_leave(dispatchGroup);
}];
dispatch_group_notify(dispatchGroup,dispatch_get_main_queue(),^{
NSLog(@"All login tasks completed");
// Do whatever
});