4
votes

I need to download hundreds of files part of a media file. Lets say 1000 small downloads(1mb each). In a loop, for each file I'm creating a download task and resume it like the code below.

            NSUrlSessionDownloadTask downloadTask = session.CreateDownloadTask(request);
            downloadTask.Resume();

The tasks start fine and files are download. The problem is when the app goes to the background.

This is the session configuration that I'm using:

        sessionConfig.AllowsCellularAccess = true;
        sessionConfig.SessionSendsLaunchEvents = true;
        sessionConfig.NetworkServiceType = NSUrlRequestNetworkServiceType.Default;
        sessionConfig.HttpMaximumConnectionsPerHost = 4;
        sessionConfig.Discretionary = false;

Here's my questions:

  • Delegate methods are only called when going back to the app..or when all download tasks are finished, is this the expected behaviour? Going back to the app will trigger hundreds of events..and that takes a while!

  • Does the NSUrlSession has any limits for the amount of download tasks?

1
could you make the calls to the nsurlsession download from a background task? then use that to process the downloads?benpage
I'm not sure how that helps. Even if I call NSUrlSession from a background task I'll still have hundreds of events being triggered when I go back to the app.nhenrique
i meant have the events triggered in the background task.benpage
the events are handled asynchronously by iOS so there's no way to create you own background task to handle those eventsnhenrique
Can I ask why you want to transfer the file in small chunks rather than rely on Background Fetch to bring down the entire file? Genuine question; I have a similar problem to solve.Steve Morgan

1 Answers

0
votes

I extensively worked with Background Transfer (BT) not so long ago and could say that downloading/uploading a dozen of small files in background using BT is not an option.

BT works via separate process nsurlsessiond. It has strong limit for memory in 40MB (may depends from iOS ver or device, I don't sure). And probably in your case it will crash because of memory limit.

Best experience with the BT could be achieved when you work with big files. Download 10 files with 100 MB size will be much more effective using NSURLSessionDownloadTask. You could try to start downloading it altogether or one by one (you could do this in BG too).

About delegates... I would say that it could start your app in BG when it finished download.. or you could get all delegates when you start the app manually if BT daemon failed to open your app for some reason (memory limit). Anyway you should be prepared for any of these cases.