Problem with server side event stream when my app goes into the backg

My app responds to incoming server side event stream when in the foreground but not while in the background. Can someone point me to a resource or some examples of how to set up the URLSession and URLRequest? Remember, I've got this working when the app is in the foreground.

Replies

The basic rules of the road are covered by Technote 2277 Networking and Multitasking, that is, networking works fine in the background as long as your app is running. Most iOS apps get suspended shortly after moving into the background, and once your app is suspended your code isn’t running in order to process network events.

There isn’t a general-purpose way to prevent your app from being suspended. How you continue here depends on the specifics of your app. A solution that works for many folks is to transition from using a network connection to hear about new events to using push notifications.

Share and Enjoy

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

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

Thanks Quinn. I agree the right solution is to employ APS. I was just surprised that if I have the server generate an event a few seconds after my app transitions to the background the event is not received. Perhaps the stream is killed immediately after the transition to bkg?

If you read TN2277, you’ll find that I was deliberately vague about when socket resource reclaim would run. That’s because it’s an implementation detail. On older system, the only reliable way to trigger it was to screen lock the device. On current system, it runs as soon as your app get suspended.

Perhaps the stream is killed immediately after the transition to bkg?

To be clear, it’s not about going to the background, it’s about being suspended. If your app remains running in the background — using either a special background mode or a

UIApplication
background task — your connections will continue running happily.

Share and Enjoy

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

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

Thanks again. Yes, I implemented a background task and the server side event stream is happily being processed when the app is in the background. I realize that this is not a substitute for going the push notification route since I will only get a limited amount of time before being suspended but that's ok.


Again much obliged Quinn! (I don't know if you remember me - I used to be an engineer in ATG and Kim Silverman's speech group back in the day (was at Apple from 1988 - 2004). We met a LONG time ago!). Cheers!

I realize that this is not a substitute for going the push notification route since I will only get a limited amount of time before being suspended but that's ok.

Make sure you test your app on iOS 13 beta. My understanding is that the

UIApplication
background task execution time limits have changed there (once I get around to confirming that I’ll update my UIApplication Background Task Notes post.

I don't know if you remember me

DevForums just gives me your user name, QBit47, and it’s hard to recognise folks based on that (-:

Share and Enjoy

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

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