Is there a way to remove the delay when [NSURLSessionConfiguration backgroundSessionConfiguration] is used?

StackOverflow https://stackoverflow.com/questions/21871017

Pergunta

I am trying to run a background NSURLSessionDownloadTask. However, I noticed that it takes approximately 30 seconds before the task actually begins. When I use the defaultSessionConfiguration, the task starts almost immediately. Is there a way to trigger the NSURLSessionDownloadTask immediately when using the backgroundSessionConfiguration?

Below is the result I get when I use backgroundSessionConfiguration:

NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfiguration:[NSString stringWithFormat:bgIdentifier, self.urlModel.code]];

Here are the log output. Note that “Start uploading…” is logged right before calling the “resume” method and “Upload progress” is logged in the NSURLSessionTaskDelegate’s didSendData method.

2014-02-18 20:13:55.403 myapp[396:60b] [DoneViewController] Start uploading...
2014-02-18 20:14:17.699 myapp[396:60b] [DoneViewController] Upload progress - 1%
2014-02-18 20:14:17.704 myapp[396:60b] [DoneViewController] Upload progress - 2%
2014-02-18 20:14:17.705 myapp[396:60b] [DoneViewController] Upload progress - 3%
2014-02-18 20:14:17.706 myapp[396:60b] [DoneViewController] Upload progress - 4%

Just for reference, the same thing but with the default session configuration.

NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];

You can see there is no delay on console result is like this:

2014-02-18 19:57:00.552 myapp[376:60b] [DoneViewController] Start uploading...
2014-02-18 19:57:00.707 myapp[376:60b] [DoneViewController] Upload progress - 2%
2014-02-18 19:57:00.710 myapp[376:60b] [DoneViewController] Upload progress - 5%
2014-02-18 19:57:00.711 myapp[376:60b] [DoneViewController] Upload progress - 8%
2014-02-18 19:57:00.711 myapp[376:60b] [DoneViewController] Upload progress - 11%

Edit: With iOS 7.1, there are times when there are no delays. However, there are still times where there are about 20 second delays.

Foi útil?

Solução

Though Apple doesn't document it at all, this is how background sessions are designed. You simply cannot take over the network connection while another app is running. iOS/Apple is in charge of running your background uploads/downloads in the way it feels will provide the best user experience. Imagine being the running app and having the network connection clogged by another app while trying to make basic HTTP calls. I've written more about my experience and when to use this feature. https://medium.com/@KyleRStewart/zombie-uploads-with-ios-dd3b1f6b66

Outras dicas

-(NSURLSession *)backgroundSession // For Background Download
{
static NSURLSession *backgroundSession = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration backgroundSessionConfiguration:@"com.shinobicontrols.BackgroundDownload.BackgroundSession"];
    backgroundSession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
});
return backgroundSession;
}

For More check this LINK. You can see here there are various methods of downloading. I hope you'll get your solution here.

Can you try this?

config.discretionary = NO;

According to the docs, it:

determines whether background tasks can be scheduled at the discretion of the system for optimal performance.

Caveat: you can only do this while the app is in the foreground.

https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLSessionConfiguration_class/Reference/Reference.html#//apple_ref/doc/uid/TP40013440-CH1-SW31

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top