Network.Framework vs URLSession in Background Task (BGTask)

As noted here, https://developer.apple.com/forums/thread/116799 the Network framework probably won't have a connection available when running in the background.

We've been using the BGTask for a couple years now to start a URLSession and pull data from a web server. It works very nicely and reliably.

Do we have any options if we want to connect to another iPad, though? I ran a test and even if I have a "server" iPad running a Network framework listener (NWListener), and the app is in the foreground and the screen on, a "client" iPad (NWBrowser) cannot connect to the NWListener when trying to connect from the BGTask; it gives a DefunctConnection error.

Why does the Network framework not have the network available to it, but a URLSession does?

Is this a limitation of the iPad, or the Network framework? If I had an iPad running as a web server like this project,

https://github.com/swisspol/GCDWebServer

and an iPad client tries to connect a URLSession to it, would that work?

If this is an iPad limitation, could I use a MacBook on the network as a web server and connect to that instead?

  • UPDATE: If I run a web server on another iPad using Telegraph as described here, https://github.com/Building42/Telegraph the BGTask is able to connect to that iPad and run a URLSession to download from it. (The iPad acting as a web server does need to be on with the server app running; the iPad can't be turned off and the app can't be in the background, so it's not like a true server in that regard.) That tells me that the Network framework doesn't have network access like URLSession does.

Add a Comment

Accepted Reply

The task will definitely be running from a BGTask at night, which means the app will be suspended (and therefore also backgrounded).

Huh? A BGTask running at night is most likely a background processing task. Such a task keeps your app running in the background for the duration of the task, meaning you’re free to use any networking API.

Just make sure you set the requiresNetworkConnectivity property in your request.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Replies

Way back in the day I wrote a technote about this: Technote 2277 Networking and Multitasking. In short, if you ignore URLSession background sessions, which are special, networking in the background:

  • Works fine as long as your app is running in the background

  • Will suffer a variety of problems if your app gets suspended

So, this issue is more about whether your app is running than about networking.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Hi Quinn! The task will definitely be running from a BGTask at night, which means the app will be suspended (and therefore also backgrounded). Am I correct to assume that URLSession is the only way to communicate to the "outside world", so to speak? Because at that point any PeerConnections established with the Network framework will probably not be usable?

We're trying to determine if the Network framework is a viable way for the app to fetch data using a BGTask, or if we will need to set up a web service for the app to communicate with. From my quick experiments, it seems like a suspended app running a BGTask can only use a URLSession at that point. It's interesting that you mentioned that URLSession is special--is that because it's given networking allowances that other frameworks (like Network) don't have?

The task will definitely be running from a BGTask at night, which means the app will be suspended (and therefore also backgrounded).

Huh? A BGTask running at night is most likely a background processing task. Such a task keeps your app running in the background for the duration of the task, meaning you’re free to use any networking API.

Just make sure you set the requiresNetworkConnectivity property in your request.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Thank you so much for that info! That helped a ton. What steered me wrong was that the NWConnection would never stay connected once the app gets backgrounded, and even when I tried using it during the BGTask, it wasn't working. However, when I would start a new NWConnection with the NWListener, it did work, proving that what you said about the network being fully available during the BGTask, as long as requiresNetworkConnectivity was set to true like you said. Is it a better strategy, then, to consider the NWConnection as short-lived, rather than trying to keep it alive? That is, should I use the NWConnection to connect, do the necessary business, then close it, and start a new one the next night? These tasks will be occurring once each night. I'm also curious what you meant about the URLSession being special--did that mean it's given extra permissions or capabilities of some kind?

Is it a better strategy, then, to consider the NWConnection as short-lived, rather than trying to keep it alive?

You can’t maintain an NWConnection across your app being suspended. It’s also poor form to maintain an NWListener across suspension. These are just the Network framework versions of the rules outlined in TN2277. That technote also discusses various ways you might handle this.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"