我正在尝试拉出用户的推文句柄并将其填充到tableview中的文本字段中。请求成功完成并调用执行块,但UI需要几秒钟才能更新。我已经尝试使用NSNotifications来触发UI更改,但是在更新UI时仍然有延迟。下面是我用来从ACAccountStore中提取信息的代码,我用于表的唯一特殊类是自定义UITableViewCell。还有其他人看到这个吗?或者我错过了导致运行时问题的东西?
- (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");
}
答案 0 :(得分:1)
它是否真的立即调用'twitterDictionaryHandler'函数?
如果是这样,但暂时不更新UI那么它似乎是iOS的常见问题。您需要做的是将UI的更改放入GCD(Grand Central Dispatch)主队列,以便它具有优先级并尽快完成。我在下面为其他可能遇到此问题的人举了一个例子。
dispatch_async(dispatch_get_main_queue(), ^{
username.textField.text = dictionary[@"screen_name"];
});