Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: server calls in background often lead to timeout

My app makes some server calls whenever it is started, now I face a weird problem: when a push notification with content_available wakes up my app the calls are made as well, but more often than not they run into a timeout.

Does this have to do with the app being in the background? Is something configured wrong? I activated background modes background fetch as well as remote notifications. Am I missing something?

like image 663
swalkner Avatar asked Jan 19 '18 10:01

swalkner


2 Answers

Am I missing something?

It's very hard to know since you haven't shown any code, but it sounds like you're not setting up your network session with the configuration returned from URLSessionConfiguration's background(withIdentifier identifier: String) method. You can't just fire off requests when your app is in the background the same way you would in the foreground; using the background configuration lets the system manage the transfer on your app's behalf.

like image 132
Caleb Avatar answered Oct 23 '22 15:10

Caleb


You have to use NSUrlSession for connecting while in background. Make sure you use it instead of NSURLConnection or any other connectivity library that is not backed by the session. While in background your app can be suspended and re-activated many times and the session in the only facility that can properly handle this.

like image 30
Terminus Avatar answered Oct 23 '22 15:10

Terminus