LettersBa
LettersBa

Reputation: 767

NSURLSession with a performSelector:

I found out that iOS can run processes even when your application is in background mode (minimized) in at most 10 minutes, and to work around that and make it run forever, it would be necessary:

Apps that need to download and process new content regularly

And that's what using NSURLSession API to make requests to my server:

-(void)SendFiles{
...
    NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

            if(error == nil){
            [self performSelector:@selector(SendFiles) withObject:nil afterDelay:3600.0];
            }

            }];

        [dataTask resume];

}

Some of us know that to run processes even when the app is minimized, we should use some tags, but if you are using the API NSURLSession is no longer needed this kind of thing, right?

In the case of my project, he needs to make such a request every 1 hour on my server, and for this I added a code within the block completionHandler: to be called every 1 hour the same method that executes the send command files to the server.

My question is: The time of 10 minutes can affect the selector with delay of 3600 seconds? causing my app is fully down, and I can not send files to the server?

Upvotes: 1

Views: 282

Answers (1)

Rob
Rob

Reputation: 437622

A couple of thoughts:

  1. The ten minute window was reduced to three minutes in iOS 7.

  2. You only get that window if you request it with beginBackgroundTaskWithName (or, prior to iOS 7, beginBackgroundTaskWithExpirationHandler). See the Executing Finite-Length Tasks section of the App Programming Guide for iOS.

    Note, I believe that this background state behavior changes whether you run the app connected to the Xcode debugger, so make sure to test this on an actual device, and run the app directly from the device, not through Xcode.

  3. Yes, this limited amount of time will affect your ability to call performSelector:afterDelay: with a delay of 3600.0.

  4. If you want to have the app periodically ask the server for data, you would nowadays use "background fetch". See the Fetching Small Amounts of Content Opportunistically in the App Programming Guide. This is designed to poll a server to see if there is data to be downloaded.

    You only have 30 seconds for this request to be performed. You also have no control over the timing of these requests (though whether you report whether there was data available or not may affect the timing of the subsequent request).

Upvotes: 2

Related Questions