0

I have read few articles but could not find what I was looking for so here's my query.

I am downloading few files from the server and there is a case where the user locks his screen, in this case the ios device loses its network connectivity and the file sync fails.

I have read few article on NSURLSession but it is available from iOS 7 onwards and the app I am working on supports from iOS 6 and later.

So is there a way where I can download 20 or 30 files in the background or when the user hits the lock screen in a generic fashion without having to worry about which OS version I am supporting.

As of now I have read that we have 30 seconds to perform network activity so is there a limitation to the number of server calls in these 30 seconds?

About my code, I am having a class named as DownloadFiles which calls a service and the service returns me an array of fileURL and using NSData I am fetching these files and saving them in the doc directory, so while implementing the background call thing do I need to pass the index of my array which will detect the current file which is downloading and then carry on from the next index.

  for(NSDictionary *dict in filearray) {
     NSString *fileURL = [[dict valueForKey:@"FileURL"]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
     NSData *fileData  = [NSData dataWithContentsOfURL:theFileURL];

     if (fileData.length==0 || fileData==nil || theFileURL==nil) {
        NSLog(@"empty file URL = %@",theFileURL);
     }
     if (fileData.length!=0){
        BOOL savefile = [fileData writeToFile:[HTML_SERVER_FILES stringByAppendingPathComponent:[dict valueForKey:@"FileName"]] atomically:YES];
        if (savefile!=YES) {
           NSLog(@"Not saved file = %@",theFileURL);
        }else{
           NSLog(@"file saved at path %@",HTML_SERVER_FILES);
        }

        fileData = nil;
    }
}

Please let me know what needs to be done in this case.

MLavoie
  • 9,671
  • 41
  • 36
  • 56
user2538944
  • 305
  • 4
  • 12
  • Are you sure you can't require iOS7? It's on [90% of devices](https://developer.apple.com/support/appstore/) already, and iOS 8 is just around the corner, which is likely to prompt a further ditching of older devices and therefore older OS versions. NSURLSession's background features are probably the best solution if you can use them. – Matt Gibson Aug 02 '14 at 18:25
  • @MattGibson : Yes you are right, but the client wants his app to support iOS 6 as well. Also do you have any idea regarding the number of calls that can be made using NSURLSession as in i am trying to download 20-30 files but between this if the user locks his device then in that case how can we use NSURLSession. – user2538944 Aug 03 '14 at 05:52
  • 1
    I've never tried such a high number of files. Generally if you use a background session in NSURLSession the download will continue in the background if the user locks their device. However, the downloads may be slowed—background transfers aren't treated with priority. They *should* finish eventually, and you should be able to queue a bunch (and set a max connections per server so they only come one at a time, say) but you'll have to experiment. Watch "What's new in Foundation Networking" from this year's WWDC to get a feel for it. There's a section on background transfer. – Matt Gibson Aug 03 '14 at 06:00
  • Fundamentally, though, iOS takes your app being exited and the phone being locked as hints that the user doesn't think your app is that important at the moment, and doesn't guarantee to finish your downloads. You'll be notified of cancellations and failures even if your app is suspended/killed, though, through app delegate notifications that allow you to reconnect your session and receive any important session/task notifications that you missed. – Matt Gibson Aug 03 '14 at 06:03

0 Answers0