Search code examples
phpiosafnetworkingafhttprequestoperation

Forcing return of response object to AFNetworking


I'm using AFNetworking to run a PHP script as shown below:

NSString *urlString = @"scriptURL.php";
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:urlString parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    ...
} success:^(AFHTTPRequestOperation *operation, NSDictionary *responseObject) {
    NSLog(@"it worked!");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"error: %@", error.description);
}];

However, there are parts of the script that take longer to run (sending a bunch of push notifications). Currently, this means that I do not receive a response object until after all of the pushes have been sent, causing a significant delay. Is there a way to force the return of the response object so that the longer, more time-consuming actions can run in the background? In other words, can I force a return of a response object such that it returns before I begin sending the push notifications, so that the iOS side does not need get stalled in waiting for the other actions to finish


Solution

  • As everyone will probably tell you, what you're trying to achieve has nothing to do with AFNetworking, nor with your iPhone app itself. Basically if your API is taking too long to compute what it's got to do (Sending Push Notifications in this case), AFNetworking ignores it, but is just waiting for the response.

    If I got it right you develop the PHP side by yourself, so you might want to have a quick glance to how to run a background process in PHP to achieve what you talked about.

    There are several links that might help you with that:

    And several others that might suit your needs at best. Hope this put you on the right way.