我第一次遇到这样的问题,需要您的帮助。
我正在获取twitter用户信息,并使用NSLog可以在控制台中看到它,但是当我在标签或文本视图上显示它时,则需要花费更多时间,几乎是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
内编写了代码,块将在异步线程而不是主线程中执行。您正在从那里更新UI元素。这将导致问题,因此您需要从主线程更新UI,因为您正在使用dispatch_get_main_queue
。关于iphone - 在控制台上显示时,在标签或textview上显示文本会花费更多时间,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15216645/