BGTaskScheduler for doing http tasks?

Currently we are doing it the old fashioned way, using background url session that we hit whenever app does a background fetch.

Seems like there is a better way of doing it with new framework. Are there good Apple examples of doing that? I haven't found any examples that do that.


Do we still need to use URLSessionConfiguration.background(withIdentifier: sessionName) with all the data/session delegates, or can we instead use URLSession.shared api with a completion block?


Thanks,


Chris

Accepted Reply

The BackgroundTasks framework is potentially useful for this sort of thing. In the traditional background fetch model, you were resumed in the background but only got a small execution time allowance. That meant you had to use a

URLSession
background session for your networking. With the BackgroundTasks framework you can receive large amount of background execution time, which gives you the choice of using either a background session or a standard session, whichever you prefer.

Which to use depends on the details of your networking. If, for example, you’re downloading a small number of huge files, a background session would still make sense. That’s what it was originally designed for, and it works well for that. OTOH, if you have to do a lot of interactive back and forth with your server, a standard session is a much better choice.

The main thing to watch out for here is expiry. As is normal for background execution, there’s no guarantee that you’ll get all the time you need.

Are there good Apple examples of doing that?

I dunno about good but there is an example (-: Check out Refreshing and Maintaining Your App Using Background Tasks.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

The BackgroundTasks framework is potentially useful for this sort of thing. In the traditional background fetch model, you were resumed in the background but only got a small execution time allowance. That meant you had to use a

URLSession
background session for your networking. With the BackgroundTasks framework you can receive large amount of background execution time, which gives you the choice of using either a background session or a standard session, whichever you prefer.

Which to use depends on the details of your networking. If, for example, you’re downloading a small number of huge files, a background session would still make sense. That’s what it was originally designed for, and it works well for that. OTOH, if you have to do a lot of interactive back and forth with your server, a standard session is a much better choice.

The main thing to watch out for here is expiry. As is normal for background execution, there’s no guarantee that you’ll get all the time you need.

Are there good Apple examples of doing that?

I dunno about good but there is an example (-: Check out Refreshing and Maintaining Your App Using Background Tasks.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks, I'll just use the shared session than, seems easier.

I'll just use the shared session than, seems easier.

Quite.

One thing I should have been more clear about: The BackgroundTasks framework supports two different types of requests:

My comments above are focused on

BGProcessingTaskRequest
.
BGAppRefreshTaskRequest
are intended to run for a short time, and thus an
URLSession
background session makes sense in that context.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"