現在、ユーザーがHTTPリクエスト(JSON形式)を使用してリモートサーバーに対して認証できるようにするアプリに取り組んでいます。
APIリクエストを処理するための別のクラスを作成しました。これは、アプリケーション全体で多くのことを行うためです。
ほとんどの魔法が行われるAPIRequestのメソッドは次のとおりです。
-(void)send
{
self.isLoading = YES;
request_obj = [[self httpClient] requestWithMethod:method path:extendedResourcePath parameters:params];
AFJSONRequestOperation *operation = [AFJSONRequestOperation
JSONRequestOperationWithRequest:request_obj
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
self.response = [[APIResponse alloc] initWithResponse:response andJSON:JSON];
self.isLoading = NO;
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
self.isLoading = NO;
}];
// queue is defined elsewhere in the class as an instance of NSOperationQueue
[queue addOperation:operation];
}
私のコントローラーでは、ボタンが押されたときに次のように呼び出します。
// set the session params from the form values
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
self.usernameField.text, @"session[username]",
self.passwordField.text, @"session[password]", nil];
// create a new API request object
api_request = [[APIRequest alloc] initWithReourcePath:@"sessions" andMethod:HTTPMethodPOST andParams:params];
// send the request
[api_request send];
私がうまくいかないように見えるのは、リクエストが完了したことをコントローラーに通知する方法です。
APIRequestにisLoadingというプロパティがあり、リクエストがまだ行われている限り「YES」になります。だから、私はそれを尋ねることによってapi_requestが行われたかどうかを確認できることを知っています。
ただし、api_requestが完了したかどうかを確認するために、コントローラーが数秒後に応答するイベントは考えられません。
誰かがここで良いアプローチをアドバイスできますか?