Substantial time to push UIViewController in Twitter completion block

When running this code…

-(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];
    }
}];
}

There is a 5-10 second delay before the pushMainControlleris actually called, even though the account information is logged almost immediately (after pre-authorization). If I move the pushMainController call after the block however, it happens immediately, the only problem being that a user isn’t necessarily logged in at that point. I understand that it can take a second for a block to have a response due to variables such as network connectivity, but can someone help me understand this?

  • Parsing Search Result with MGTwitterEngine in Objective C
  • Twitter - how to get user's timeline
  • Is it possible to bypass the tweet dialog and post directly to twitter?
  • Change the delegate of MGTwitterEngine
  • How to “follow” people with the new Twitter API in iOS 5?
  • iOS5 - sharekit for twitter issue
  • Solutions Collect From Internet About “Substantial time to push UIViewController in Twitter completion block”

    The completion block is not being done on the main queue. You need to ensure your UI code gets done on the main thread:

    [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];
            });
        }
    }];
    

    You may have to wrap the showError call as well.