HTTPリクエスト(非同期で実行される)を使用して取得したデータを入力したい静的UITableViewがあります。いくつかの場所で HTTP リクエストを作成しようとしましたが、毎回リクエストからのデータの到着が遅すぎて、テーブル ビューにデータが入力されません。テーブルビューにデータを入力する唯一の方法はreloadData
、次のように呼び出すことです。
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
_accountModel = [[CatapultAccount alloc] init];
[_accountModel getCurrentClient:^ (BOOL completed, NSDictionary *currentAccount) {
if (completed) {
_account = currentAccount;
[self.tableView reloadData];
} else {
UIAlertView *errorMessage = [[UIAlertView alloc] initWithTitle:@"Account retrieval unsuccessful"
message:@"Unable to retrieve current account information"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[errorMessage show];
}
}];
}
リクエストをメイン スレッドで実行しようとしましたが、既にメイン スレッドで実行されているようです…getCurrentClient
バックグラウンドで実行される内部から呼び出された実際のリクエストだと思います。
- (void)getCurrentClient:(void (^)(BOOL completed, NSDictionary *account))completion
{
__block BOOL operationSuccessful = NO;
__block NSDictionary *currentClient = nil;
NXOAuth2Account *currentAccount = [[[NXOAuth2AccountStore sharedStore] accounts] lastObject];
[NXOAuth2Request performMethod:@"GET"
onResource:[NSURL URLWithString:[NSString stringWithFormat:@"%@/clients/%@", kCatapultHost, currentAccount.userData[@"account_name"]]]
usingParameters:nil
withAccount:currentAccount
sendProgressHandler:nil
responseHandler:^ (NSURLResponse *response, NSData *responseData, NSError *error) {
if (error != nil) {
operationSuccessful = NO;
#if DEBUG
NSLog(@"ERROR: %@", error);
#endif
}
else {
operationSuccessful = YES;
NSError *jsonError;
currentClient = [NSJSONSerialization JSONObjectWithData:responseData
options:kNilOptions
error:&jsonError];
if (jsonError != nil) {
operationSuccessful = NO;
#if DEBUG
NSLog(@"Error: %@", jsonError);
#endif
}
}
completion(operationSuccessful, currentClient);
}];
}
それで、私のアプローチ(reloadDataを呼び出す)は良いものですか?リクエストが完了するまでテーブル ビュー コントローラを待機させる方法はありませんか?