Group Group Group Group Group Group Group Group Group

Networking with URLSession · Background Sessions in Action |

This is a companion discussion topic for the original entry at

Hi Brian, Can you help with something? If you pause and resume a download more than once, it frezees when you try to resume. I didn’t notice before because the downloads of halftunes are superfast in reason of the size of files. But i used the halftunes approach to my own project that download a podcast episodes and i having the trouble. Apparently after the second time you pause the download, the resume data produced can’t restart the download. Can you help me to solve this issue? Thanks!

@bdmoakley Can you please help with this when you get a chance? Thank you - much appreciated! :]

This was discussed in another topic.

Hello Brian, there is very little documentation or sample code about uploading in the background and handling the callback after the server returns a response for the upload when the app is terminated by the user. All the examples that are out there are for downloading in the background, even in the official documentation. Could you please help in some code sample for the same.

@bdmoakley Can you please help with this when you get a chance? Thank you - much appreciated! :]

I don’t believe we covered background uploading. Try following the same procedure for background downloading, but instead, use an upload task. Give it a shot and see what you find.

I followed your background downloading tutorial ( and converted it to background uploading. But somehow it is not working for me, when i put app in background. When I debug using Xcode, am getting " Could not load IOSurface for time string. Rendering locally instead." this message and it stops uploading but when i bring the app back in foreground it start uploading.

I am expecting my app to continue uploading files while it is in background as well as when the user closes the app. Could you please help? Thanks a lot!


@nareshkumowiz20 Do you still have issues with this?

Yes, but right now we are not focusing on it, coz it already taken our too much time. But if you have the answer then it will great to know.

Your best bet is to reach out on the Apple developer forums to see what’s happening.

In the handle events method in AppDelegate, I noticed that you are creating a new Instance of DownloadService instead of using the instance that is available in SearchViewController.

  1. DownloadService instance created in handle events method in AppDelegate is local to the method. Does it get deallocated after the method returns?

  2. What is the difference between the instance of DownloadService in SearchViewController and AppDelegate? I modified the Appdelgate method to look like below. Do you think it will have issues?

      func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) {
        backgroundSessionCompletionHandler = completionHandler
        let searchViewController = SearchViewController()
        let sessionConfiguration = URLSessionConfiguration.background(withIdentifier: identifier)
        let downloadSession = URLSession(configuration: sessionConfiguration, delegate: searchViewController, delegateQueue: nil)
        searchViewController.downloadService.downloadsSession = downloadSession
1 Like

@bdmoakley Can you please help with this when you get a chance? Thank you - much appreciated! :]

Hi, Brian,

Thanks for setting up the half-tune project. I learned a lot from it and I will continue playing with it.

I have a question regarding the func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) method.

Based on your video tut, it sounds like that this method will be called “either when the download has completed in the background or failed with error”.

I set up a breakpoint inside the func but the debugger has never hit it. Also, I hit tap a bunch of items in the list to download and switch to another app, it does not hit the breakpoint as well (my env, Xcode 11.4.1, iOS, 13.4, iPhone XR).

I read the official doc which says, “receiving this event indicates that all messages previously enqueued for this session have been delivered” but I was even more confused.

So I am just wondering, under what scenarios, will this function be called? Can you give an example? Thx



Hi @oaktree,

I’ve found that this is called when you download a file in the background using a background configuration. This method won’t be called if you create a urlsession using regular configuration object.

One way to call this is to initiate a background transfer and then go to the home screen. Just wait until the background transfer and the method should be called. If it’s not tripping, then you may be downloading too fast. You can activate a link conditioner to slow down your download. Print out your progress to the console to know when the download is completed, and thus, when the method is fired.

I hope that helps!

Hi, Brian,

Thanks for getting me back and sorry for the late reply. You are 100% correct, the cause is that the download speed is too fast, after I dialed it down from link conditioner, the app hit all the break points that I set. I think I have better understanding on this now.

Another question, in the appDelegate a completionHandler is passed and stored in func(... handleEventsForBackgroundURLSession ...). So is there any implementation in this handler? Does this handler actually do anything?

Thanks again


Hey there,

Sorry for my late reply. I just got back from vacation :slight_smile: The handler being passed to the function is a completion handler provided by the operating system. It lets the iOS know that the background download is complete. This basically shuts down all the resources associated with that background transfer.

If you don’t call it, iOS will hold the resources for you. So basically, when you are done downloading make sure to call the completion handler.

We just updated the URL Session course and you can see this in action here:


Hi, @bdmoakley

No worries Brian, hope you had a great time :wink: I see it now and that’s simple. Just curious, so if you don’t call the handler, would the system release the resource after certain amount of time or hold it until you the handler is called?

I definitely will check the new new urlsession course later on!



@bdmoakley Can you please help with this when you get a chance? Thank you - much appreciated! :]

This part is super hard to follow for a beginner…