我第一次遇到这样的问题,需要你的帮助。
我正在获取 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/