UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Background Task

Forums > iOS

Hello everyone. I want to add background capability to my iOS app. I did everything mentioned in the documentation to add background app refresh to my app and if I schedule the task manually in the debuger, it works. The problem is my task is never gets executed by the system and I have no idea how to fix it. I've changed the earliestBeginDate to 5 minutes and send a local notification with the task is executed but nothing is happening.

I have also another question. I want to execute a query on Health kit data in the background. I've tried health kit background delivery with observer query but it's not working as expected and I don't know when It's called.

What's the problem with my background app refresh task? How am I supposed to mix background task and health kit background delivery?

Thanks

3      

It's been a few years that I experienced with the background refresh feature, but I don't think things have changed, so I hope this helps : Here's what Apple writes about Background refresh : "Apps that update their content frequently, such as news apps or social media apps, can use this feature to ensure that their content is always up to date. Downloading data in the background before it is needed minimizes the lag time in displaying that data when the user launches the app."

And how does iOS decide when you're app is allowed to download in the background (using this feature) : it's based on the usage of the app. If the user opens your app let's say every day between 8 and 8h30, then after a few days, you will see that the background refresh feature reactivates your app, maybe before 8, probably also between 8 and 8h30. So the app can download the content before the user even opens the app. Sounds nice but it's actually useless.

A bit better (or even a lot better) is to use silent remote notifications. It's the same feature but your app is reactivated because a push notification is sent to your app. You can use a silent notification that the user doesn't hear or see, it just activates the app , see Apple documentation. I experimented with that too using the service offered by Quickblox. That worked quite well actually.

There are other methods, as described here, see respond to important events in the background. But you probably don't need any of these like bluetooth, location, play audio etc.

An app in the background can not read from HealthKit, only write.

Be aware also that if your user closes the app (force close), then it's done, there's no way to reactivate the app in the background.

And for other reasons iOS may decide to kill your app, like if it consumes too much battery or memory.

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.