iphone - 在标签或 TextView 上显示文本在控制台上显示时花费更多时间

标签 iphone ios twitter nsstring uilabel

我第一次遇到这样的问题,需要你的帮助。

我正在获取 Twitter 用户信息,使用 NSLog 我可以在控制台中看到它,但是当我在标签或 TextView 上显示它时,它需要更多时间,几乎需要 1 分钟。

我使用的代码是....

_accountStore = [[ACAccountStore alloc] init];
            ACAccountType *accountType = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

 [_accountStore requestAccessToAccountsWithType:accountType options:nil
                                                completion:^(BOOL granted, NSError *error)

             // Request access from the user to use their Twitter accounts.

             {
                 // Did user allow us access?
                 if (granted == YES)
                 {
                     // Populate array with all available Twitter accounts
                     NSArray *arrayOfAccounts = [_accountStore accountsWithAccountType:accountType];
                     [arrayOfAccounts retain];

                     // Populate the tableview
                     if ([arrayOfAccounts count] > 0)
                         NSLog(@"print %@",[arrayOfAccounts objectAtIndex:0]);
                     //
                     ACAccount *twitterAccount = [arrayOfAccounts objectAtIndex:0];

                     NSString *userID = [[twitterAccount valueForKey:@"properties"] valueForKey:@"user_id"];
                     NSLog(@"print user id is %@",userID);// Here i can see immediately 

                    testLabel.text=[NSString stringWithFormat: @"Hi ,%@",userID];// Here it is taking more time...

最佳答案

您正在从异步线程更新您的 UI 元素。这就是问题发生的原因。

替换:

testLabel.text=[NSString stringWithFormat: @"Hi ,%@",userID];

与:

dispatch_sync(dispatch_get_main_queue(), ^{

   testLabel.text=[NSString stringWithFormat: @"Hi ,%@",userID];

});

记住:您应该只从主线程更新您的 UI 元素。

在您的情况下,您在 block 中编写代码, block 将在异步线程中执行,而不是在主线程中执行。您正在从那里更新您的 UI 元素。这将导致问题,因此您需要从主线程更新 UI,因为您正在使用 dispatch_get_main_queue

关于iphone - 在标签或 TextView 上显示文本在控制台上显示时花费更多时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15216645/

相关文章:

iphone - 我可以给我的 iPhone 一个域名吗?

ios - UIView subview 未出现

python - 解析 JSON 时彻底修复 KeyError NoneType

jquery - 如何在 Bootstrap 中将一列堆叠在其他两列之上?

iphone - 技巧 preferredInterfaceOrientationForPresentation 在 vi​​ewController 更改时触发

iphone - 如何在通过 Storyboard 使用静态内容时更改分组 tableView 标题部分中的字体样式

ios - 不关闭 AlertController 的 UIAlertController 上的操作? (禁用解雇)

hadoop - Flume Twitter 流媒体问题

iphone - NSFetchedResultsController 中的自定义排序

ios - iOS 应用程序是否将 NSUserdefaults 信息保存在同一个地方?