2014-02-25 25 views
0

slrequest performrequestwithhandler摊位后,我试图拉用户的Twitter句柄,并填充到一个tableview中一个文本框。请求成功完成并调用执行块,但UI需要几秒钟才能更新。我已经尝试使用NSNotifications来触发UI更改,但是在更新UI方面仍然存在延迟。下面是我用从ACAccountStore,我使用表的唯一特殊类是提取信息的自定义的UITableViewCell的代码。有没有人看到这个?或者我错过了导致运行时问题的东西?更新文本框与Twitter手柄

- (IBAction)connectTwitter:(id)sender { 
     UISwitch *sw = (UISwitch *)sender; 
     if (sw.isOn == NO) { 
      return; 
     }  
    ACAccountStore *account = [[ACAccountStore alloc] init]; 
    ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; 
    __weak MBSuitUpViewController *weakSelf = self; 
    [account requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) { 
     if (granted == YES) { 
      NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType]; 
      if (arrayOfAccounts.count > 0) { 
       ACAccount *twitterAccount = [arrayOfAccounts lastObject]; 
       NSURL *requestURL = [NSURL URLWithString:@"https://api.twitter.com/1/account/verify_credentials.json"]; 
       SLRequest *user = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:requestURL parameters:nil]; 
       user.account = twitterAccount; 
       [user performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { 
       NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:nil]; 
        [weakSelf twitterDictionaryHandler:json]; 
        weakSelf.userInfo = @{@"name": json[@"name"], @"handle":json[@"screen_name"]}; 
      }]; 
      } else { 
       UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"No Twitter Account Found" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Okay", nil]; 
       [alert show]; 
      } 
     } else { 
      NSLog(@"Permission denied %@", error.localizedDescription); 
     } 
    }]; 
} 

- (void)twitterDictionaryHandler:(NSDictionary *)dictionary { 
    MBSuitUpCell *username = (MBSuitUpCell *)[self.view viewWithTag:2]; 
    username.textField.text = dictionary[@"screen_name"]; 
    NSLog(@"Should update textfield"); 
} 

回答

1

它实际上是否立即调用“twitterDictionaryHandler”函数?

如果这样做,但不更新了,而UI则似乎是与iOS的一个常见问题。你需要做的是将UI到GCD(大中央调度)主队列的变化,所以具有优先权,尽快完成。我已经为其他可能有此问题的人举了一个例子。

dispatch_async(dispatch_get_main_queue(), ^{ 
    username.textField.text = dictionary[@"screen_name"]; 
}); 
+0

它实际上会立即调用twitterDictionaryHandler函数。由于我保存用户信息,我试着用tableview重新加载它,检查用户信息。我记录的块和表重载的端部,看到从块表重载的端部11秒的延迟。 –

+1

感谢使用GCD运行它的建议,我撞着twitterDictionaryHandler到主队列和它的第二内更新。 –