iOS: server calls in background often lead to timeout
Asked Answered
T

2

7

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?

Tribromoethanol answered 19/1, 2018 at 10:31 Comment(2)
If your network task will take more than a few seconds you might need to call beginBackgroundTaskWithExpirationHandlerWulf
ok, that makes sense - but it seems as if the error block is called in my network calls which surprises me...Tribromoethanol
E
6

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.

Ergo answered 23/1, 2018 at 3:21 Comment(0)
A
1

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.

Aeriform answered 30/1, 2018 at 2:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.