iosobjective-ctwitter

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?


Solution

  • 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.