Run Request In Background IOS

1.2k views Asked by At

I want to run some request while my app is in background.My App supports background mode -> background fetches.

But below code is always return failure while my app is in background! Is there a way to run request?I also tried NSURLConnection it sometimes receives response sometimes does not receive response!

 [[AFHTTPRequestOperationManager manager]POST:@"http://app.xxxx.com/message_delivery.php" parameters:@{@"message_id":MsgID} constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {


    } success:^(AFHTTPRequestOperation *operation, id response) {
        NSLog(@"succesfuly tik");
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"%@",error);
    }];

These code sometimes working sometimes not

  NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"http://app.xxxx.com/message_delivery.php?message_id=%@",MsgID]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url
                                              cachePolicy:NSURLRequestUseProtocolCachePolicy
                                          timeoutInterval:2];
    [[[NSURLConnection alloc] initWithRequest:request delegate:self]start];
1

There are 1 answers

0
Rob On

Background fetch is a very specific technology that lets the OS opportunistically call your code, which in turn calls your web service to see if there is any new data to retrieve. But you don't have much time to perform that request (30 seconds?). It's not intended for anything substantive (or if it is, you'd marry background fetch with background NSURLSession, a completely different technology). And when background fetch does one of these opportunistic calls, you call the completion handler when you're done.

But your two code samples don't show us your calling the completion handler passed to you by the app delegate's performFetchWithCompletionHandler. So I'm unclear whether you're really doing background fetch and haven't shared the relevant code, or whether you're conflating different technologies.

There are three basic background networking approaches, each with its own pros and cons.

  • There is background fetch, where the OS will initiate an opportunistic network request at a time of its own choosing;

  • There is beginBackgroundTaskWithExpirationHandler, which lets your app continue running for a few minutes (nowadays 3 minutes) to finish up finite length tasks ... this is useful if you're starting a network request which you know may take up to a few minutes and you don't want it to die just because the user left your app; and

  • There is NSURLSession with a background NSURLSessionConfiguration, in which an iOS daemon performs the requests on behalf of your app and lets you do upload and download requests that might take much longer (even after your app has been terminated), waking up your app when its done.

It's not clear to me, on the basis of your description, which of these are applicable in your case. When you say that your requests are running in the background, how precisely are the requests initiated and how long are they likely to take, worst case scenario?

Having said that, when doing background networking, I would consider shifting to NSURLSession, which opens up additional opportunities. With AFNetworking, this is achieved with AFHTTPSessionManager (rather than AFHTTPRequestOperationManager; it's a little complicated but see https://stackoverflow.com/a/21359684/1271826 for a discussion of configuring AFNetworking for background sessions). If doing it yourself, it's using NSURLSession with background NSURLSessionConfiguration rather than using NSURLConnection.