Network.framework for peer to peer connection in background mode

created an iOS application which use Network.framework to connect other peer devices and then try to sync information. Everything seems working fine except one problem which is related to background mode.

Steps are here :

  1. Device1 connected with Device2
  2. sending data through Device1 , immediately i can see same data sync in Device2
  3. When Device2 put into the background mode and Device1 sending data , Device2 once come into foreground mode ,not able to receive data. [ how can i ensure data receive in other Device2 successfully once it come online [ or operate in foreground mode]

Kindly help

Answered by DTS Engineer in 729393022

1. is the Network framework solely for user-centric activities where sessions must be started and maintained with foreground app usage and it doesn't support background activity ?

The thing to keep in mind with networking in iOS is that the background/foreground state isn’t key, but rather the suspended/running state. Networking works just fine as long as your process is running. Once it’s suspended, everything just stops [1].

Now this does tie into the background/foreground state because iOS are typically suspended when they move to the background. However, there are circumstances where that doesn’t happen, for example, an audio streaming app.

Whether this distinction is relevant to your app is hard to say without knowing more about your app.

2. Application coming from background and not able to receive data , how to ensure this ? ( In other words how to handle the reconnection issue ) do we need to find some sort of algorithm to handle this ?

Network framework doesn’t do anything to help you out here. If you want to handle this, you’ll need to structure your code to do so. One key part of that is being able to uniquely identify your peers, so that you know that the connection by peer A is a reconnection of peer A, not a new connection from peer B.

Don’t use the IP addresses to uniquely identify peers. Those change all the time.

3. Is there any limitation of how many devices can work in peer to peer using Network framework ?

There’s no limitation imposed by Network framework.

There are practice limitations imposed by the underlying link layer. For example, putting 100,000 devices on a single Wi-Fi network is not going to work. The practical limit for infrastructure Wi-Fi is significantly higher than the practical limited for peer-to-peer Wi-Fi.

Share and Enjoy

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

[1] Except for networking being done by other processes on your behalf, the most common of which is NSURLSession background sessions, which are run by a system process (nsurlsessiond).

My general advice is that, when your app moves to the background [1] you should shut down your networking, resuming it when you come back into the foreground.

Share and Enjoy

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

[1] Technically this should be “becomes eligible for suspension”. Network in the background is fine as long as you something is keeping your app awake.

Thanks for your response , doing the same. Once the application moves to background mode it close the connection. Kindly help me on these questions :

  1. If Device1 sent data and Device2 is in background mode - is it possible for Device2 to receive data once come back to foreground mode [considering the current implementation using TCP based connection]?
  2. If Aeroplane mode is enabled [wifi is enabled]or disabled does it have any impact in the Network.framework connection ( experienced connection lost in this case ) ?

As a reminder, I’m using “in the background” as a shortcut for “eligible for suspension”. If your app is executing in the background, networking works just like it does in the foreground.

If Device1 sent data and Device2 is in background mode - is it possible for Device2 to receive data once come back to foreground mode [considering the current implementation using TCP based connection]?

Not reliably. When the app on Device2 gets suspended, its TCP connections will likely be closed immediately [1].

If Aeroplane mode is enabled [wifi is enabled] or disabled does it have any impact in the Network.framework connection … ?

Yes. Airplane Mode disables the Wi-Fi interface. Network framework knows which interface is being used for a connection and, if that interface goes down, it closes that connection.

Share and Enjoy

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

[1] If they’re not closed immediately, they may be closed at any time while the app is suspended.

Thanks for your response on this. But again questions remain same :

  1. is the Netowork.framework solely for user-centric activities where sessions must be started and maintained with foreground app usage and it doesn't support background activity ?

  2. Application coming from background and not able to receive data , how to ensure this ? ( In other words how to handle the reconnection issue ) do we need to find some sort of algorithm to handle this ?

  3. Is there any limitation of how many devices can work in peer to peer using Network.framework ?

Accepted Answer

1. is the Network framework solely for user-centric activities where sessions must be started and maintained with foreground app usage and it doesn't support background activity ?

The thing to keep in mind with networking in iOS is that the background/foreground state isn’t key, but rather the suspended/running state. Networking works just fine as long as your process is running. Once it’s suspended, everything just stops [1].

Now this does tie into the background/foreground state because iOS are typically suspended when they move to the background. However, there are circumstances where that doesn’t happen, for example, an audio streaming app.

Whether this distinction is relevant to your app is hard to say without knowing more about your app.

2. Application coming from background and not able to receive data , how to ensure this ? ( In other words how to handle the reconnection issue ) do we need to find some sort of algorithm to handle this ?

Network framework doesn’t do anything to help you out here. If you want to handle this, you’ll need to structure your code to do so. One key part of that is being able to uniquely identify your peers, so that you know that the connection by peer A is a reconnection of peer A, not a new connection from peer B.

Don’t use the IP addresses to uniquely identify peers. Those change all the time.

3. Is there any limitation of how many devices can work in peer to peer using Network framework ?

There’s no limitation imposed by Network framework.

There are practice limitations imposed by the underlying link layer. For example, putting 100,000 devices on a single Wi-Fi network is not going to work. The practical limit for infrastructure Wi-Fi is significantly higher than the practical limited for peer-to-peer Wi-Fi.

Share and Enjoy

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

[1] Except for networking being done by other processes on your behalf, the most common of which is NSURLSession background sessions, which are run by a system process (nsurlsessiond).

Network.framework for peer to peer connection in background mode
 
 
Q