Reputation: 742
I have an iOS app which uses CoreLocation and WiFi SSID information. My app was working fine until iOS 13 arrived but since then it's giving me a lot of issues especially when app goes in background. I have been using one timer in background as background task which also not work after 30 seconds and especially app got killed in background in the same time frame. I have seen some people saying that iOS 13 has been strict for background task and timing but I still have not found any direct references or links by apple which supports this claims. Is there anyone else facing the same issues then please share your insights. Thanks
I have one background task for timer:
var bgTask: UIBackgroundTaskIdentifier?
var updateTimer: Timer?
func applicationDidEnterBackground(_ application: UIApplication) {
bgTask = application.beginBackgroundTask(withName: "MyTask", expirationHandler: {() -> Void in
if let bgTask = self.bgTask {
application.endBackgroundTask(bgTask)
self.bgTask = UIBackgroundTaskIdentifier(rawValue: convertFromUIBackgroundTaskIdentifier(UIBackgroundTaskIdentifier.invalid))
}
})
DispatchQueue.main.async(execute: {() -> Void in
self.updateTimer = Timer.scheduledTimer(timeInterval: 2.0, target: self, selector: #selector(self.checkNetworkStatus), userInfo: nil, repeats: true)
})
}
@objc func checkNetworkStatus() {
print("Timer calledddd")
}
Upvotes: 5
Views: 10345
Reputation: 22926
The name background task is somewhat misappropriate.
Specifically, beginBackgroundTask(expirationHandler:)
doesn’t actually start any sort of background task, but rather it tells the system that you have started some ongoing work that you want to continue even if your app is in the background.
You still have to write the code to create and manage that work.
So it’s best to think of the background task API as raising a “don’t suspend me” assertion.
iOS 13 puts strict limits on the total amount of time that you can prevent suspension using background tasks.
iOS 13 has reduced the from-the-foreground value to 30 seconds.
a. 3 minutes, when your app has moved from the foreground to the background
b. 30 seconds, when your app was resumed in the background
You can get a rough estimate of the amount of time available to you by looking at UIApplication() backgroundTimeRemaining
property.
You may want to consider a different API to achieve your goals.
Upvotes: 9