我目前正在开发一个应用程序,它允许用户使用 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 是否完成。
任何人都可以在这里提出一个好的方法吗?