0

Facebookユーザーデータを取得して、サインアップテキストフィールドを自動補完します。

問題: テストを行ったところ、NSLog は情報をすばやく返しますが、TextFields.text の更新が遅れています。


コード:

- (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"];
        }

    }];    
}
4

1 に答える 1

1

メイン スレッドで UI の更新を実行する必要があります。完了ハンドラがバックグラウンド スレッドで呼び出されています。

[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"];
        });
    }

}];    
于 2013-04-19T16:33:47.560 に答える