在Twitter完成块中推送UIViewController大量的时间

当运行这个代码…

-(IBAction)loginWithTwitter:(id)sender { NSLog(@"Logging in with twitter"); ACAccountStore *accountStore = [[ACAccountStore alloc]init]; ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; [accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) { if (error) { [self showError:error]; return; } if (granted) { NSArray *accountsArray = [accountStore accountsWithAccountType:accountType]; if ([accountsArray count] > 1) { NSLog(@"Multiple twitter accounts"); } ACAccount *twitterAccount = [accountsArray objectAtIndex:0]; NSLog(@"%@", twitterAccount); [self pushMainController]; } }]; } 

实际调用pushMainController之前有5-10秒的延迟,即使帐户信息几乎立即(在预授权之后)被logging下来。 但是,如果我在块之后移动pushMainController调用,它会立即发生,唯一的问题是用户不一定在该点login。 我知道,由于networking连接等variables,块可能需要一秒钟才能有响应,但有人能帮我理解这一点吗?

完成块没有在主队列上完成。 您需要确保您的UI代码在主线程上完成:

 [accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) { if (error) { [self showError:error]; return; } if (granted) { NSArray *accountsArray = [accountStore accountsWithAccountType:accountType]; if ([accountsArray count] > 1) { NSLog(@"Multiple twitter accounts"); } ACAccount *twitterAccount = [accountsArray objectAtIndex:0]; NSLog(@"%@", twitterAccount); dispatch_async(dispatch_get_main_queue(), ^{ [self pushMainController]; }); } }]; 

您可能还必须包装showError调用。