1

我正在尝试实现一种方法,让用户从他们在 iPhone(iOS5 及更高版本)上设置的 Twitter 帐户中进行选择。我可以让用户名出现在 UIActionSheet 中,但由于某种原因,UIActionSheet 在调用该方法后需要大约 5 秒才能出现。

我想可能是因为检索 Twitter 帐户列表需要一段时间,但在我的日志中它们会立即出现,所以不是这样。

有任何想法吗?

- (void)TwitterSwitch {
    ACAccountStore *accountStore = [[ACAccountStore alloc] init];

// Create an account type that ensures Twitter accounts are retrieved.
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

    NSMutableArray *buttonsArray = [NSMutableArray array];

// Request access from the user to use their Twitter accounts.
    [accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {


       // Get the list of Twitter accounts.
        NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];

       NSLog(@"%@", accountsArray);


       [accountsArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

           [buttonsArray addObject:((ACAccount*)obj).username];
        }];


       NSLog(@"%@", buttonsArray);

        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
        for( NSString *title in buttonsArray)
            [actionSheet addButtonWithTitle:title];
        [actionSheet addButtonWithTitle:@"Cancel"];
        actionSheet.cancelButtonIndex = actionSheet.numberOfButtons-1;
        [actionSheet showFromTabBar:self.tabBarController.tabBar];
}]; 

}
4

2 回答 2

5

我认为问题可能在于 requestAccessToAccountsWithType:withCompletionHandler: 可以在任意队列上调用处理程序,并且您正在显示该处理程序中的 UI 元素(操作表)。由于与 UI 的交互只能在主线程上完成,这可能是个问题。我尝试将一些代码移动到另一个方法中,并在主线程上调用它——这要快得多:

- (void)TwitterSwitch {
    ACAccountStore *accountStore = [[ACAccountStore alloc] init];

    // Create an account type that ensures Twitter accounts are retrieved.
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];


    // Request access from the user to use their Twitter accounts.
    [accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {


        // Get the list of Twitter accounts.
        NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];

        NSLog(@"%@", accountsArray);
        [self performSelectorOnMainThread:@selector(populateSheetAndShow:) withObject:accountsArray waitUntilDone:NO];
        }];
}

-(void)populateSheetAndShow:(NSArray *) accountsArray {
    NSMutableArray *buttonsArray = [NSMutableArray array];
    [accountsArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

            [buttonsArray addObject:((ACAccount*)obj).username];
        }];


        NSLog(@"%@", buttonsArray);

        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
        for( NSString *title in buttonsArray)
            [actionSheet addButtonWithTitle:title];
        [actionSheet addButtonWithTitle:@"Cancel"];
        actionSheet.cancelButtonIndex = actionSheet.numberOfButtons-1;
        [actionSheet showInView:self.view];
}

请注意,我更改了显示工作表的方式,因为在我的测试应用程序中我没有标签栏控制器。

于 2012-09-01T05:44:45.720 回答
0

您始终可以使用多线程来加速您的任务。以下是您的操作方法:

dispatch_queue_t twitterAccounts = dispatch_queue_create("DisplayTwitterAccounts", NULL);
    dispatch_async(twitterAccounts, ^{
        dispatch_async(dispatch_get_main_queue(), ^{
        //Your Code goes here
        });
    });
dispatch_release(twitterAccounts);

注意 dispatch_async(dispatch_get_main_queue()): 这是因为 UIKit 调用只能发生在主线程中。(参考:斯坦福 CS193P 课程幻灯片)。

“DisplayTwitterAccounts”只是队列的名称,因此您可以在调试器/控制台中查找它。

于 2012-09-01T05:09:07.377 回答