奇怪的延迟来更新TextField [英] Strange Delay to Update TextField
本文介绍了奇怪的延迟来更新TextField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在获取Facebook用户数据以自动完成注册文本字段.
Im getting Facebook User Data to auto completing signup textfields.
问题:我做了一个测试,NSLog快速返回了信息,但是要更新TextFields.text,它会延迟.
Problem: I did a test and NSLog returns information quickly, but to update the TextFields.text it's delaying.
代码:
- (IBAction)facebookProfile:(id)sender {
if(!_accountStore)
_accountStore = [[ACAccountStore alloc] init];
ACAccountType *facebookTypeAccount = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
[_accountStore requestAccessToAccountsWithType:facebookTypeAccount
options:@{ACFacebookAppIdKey: @"417425124162461", ACFacebookPermissionsKey: @[@"email"]}
completion:^(BOOL granted, NSError *error) {
if(granted){
NSArray *accounts = [_accountStore accountsWithAccountType:facebookTypeAccount];
_facebookAccount = [accounts lastObject];
NSLog(@"Success");
[self me];
}else{
// ouch
NSLog(@"Fail");
NSLog(@"Error: %@", error);
}
}];
}
- (void)me {
NSURL *meUrl = [NSURL URLWithString:@"https://graph.facebook.com/me"];
SLRequest *meRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodGET URL:meUrl parameters:nil];
meRequest.account = _facebookAccount;
[meRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if (!error) {
NSDictionary *resultsDictionary = [responseData objectFromJSONData];
NSLog(@"%@", [resultsDictionary objectForKey:@"name"]);
// The problem is here. While NSLog runs in seconds showing Facebook User Name, the textfiend.text updates take about 10 seconds longer.
_tfName.text = [resultsDictionary objectForKey:@"name"];
_tfEmail.text = [resultsDictionary objectForKey:@"email"];
_tfGender.text = [resultsDictionary objectForKey:@"gender"];
_tfBirthday.text = [resultsDictionary objectForKey:@"birthday"];
}
}];
}
推荐答案
您需要在主线程上执行UI更新.您的完成处理程序正在后台线程上被调用.
You need to perform UI updates on the main thread. Your completion handler is being called on a background thread.
[meRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if (!error) {
NSDictionary *resultsDictionary = [responseData objectFromJSONData];
NSLog(@"%@", [resultsDictionary objectForKey:@"name"]);
// The problem is here. While NSLog runs in seconds showing Facebook User Name, the textfiend.text updates take about 10 seconds longer.
// Ensure UI updated on main queue
dispatch_async(dispatch_get_main_queue(), ^{
_tfName.text = [resultsDictionary objectForKey:@"name"];
_tfEmail.text = [resultsDictionary objectForKey:@"email"];
_tfGender.text = [resultsDictionary objectForKey:@"gender"];
_tfBirthday.text = [resultsDictionary objectForKey:@"birthday"];
});
}
}];
这篇关于奇怪的延迟来更新TextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文