I'm working on an app at the moment which allows users to authenticate to a remote server using an HTTP request (in JSON format).
I've created a separate class to handle the API Request as it's something I'll be doing a lot of throughout the application.
The method in APIRequest where most of the magic takes place is:
-(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];
}
In my controller, when a button is pressed I call:
// 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];
What I can't seem to work out is how to notify the controller that the request is complete.
I have a property on APIRequest called isLoading which will be "YES" as long as the request is still taking place. So, I know I can check if the api_request is done by asking it.
I can't think of any events that the controller would respond to a few seconds later in order to ask the api_request if it's complete though.
Can anyone advise a good approach here?