Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Daniel Santos
34,969 PointsRunning Task in Background State
I am currently working on an app that needs to keep running some task in the background, that is when the app is inactive. In summary, my app is running a Timer and the handler is calculating some data which is then reported to HealthKit and persisted with UserDefaults. The features mentioned above are working gratefully, I just want to keep running the timer in inactive state. I was looking that the background modes, but I don't feel that my app falls in any of the options there. Pasan Premaratne I was wondering if you, or anyone really, can point me to some resources.
The project is open-source so any want can check the implementation @ https://github.com/dsantosp12/Keep-Standing-iOS
I will be adding a README file soon with more details about the app.
1 Answer
Pasan Premaratne
Treehouse TeacherUnfortunately, I don't think you can do this if you want to put the app on the app store. You could select one of the background modes and be technically able to run the timer in the bg, but the app would probably be rejected.
There are very few things that Apple allows apps to do in the background for good reason (battery etc)
Daniel Santos
34,969 PointsDaniel Santos
34,969 PointsThank you very much for time Pasan. I am pretty much cloning this application and I am now wondering what approach they used. https://itunes.apple.com/us/app/varidesk-standing-timer/id889851668?mt=8
If you have a change, please take a look.
Pasan Premaratne
Treehouse TeacherPasan Premaratne
Treehouse TeacherBased on the screenshots, it looks like they're just telling you to start standing at a particular time, right? My guess is that they just create a local notification with a particular fire date and a message. They're not actually tracking standing time in the background, instead, when the app is opened with the local notification, they calculate time elapsed since start time of stand and update their display.
Once the app is in the foreground you can do whatever you want.
So let's say I want to remind you to stand every 15 minutes. Using the stop time of the current stand session, create a local notification that has start date of current time + 15 minutes. When the user opens the app with the local notification, start "tracking" stand time.
If the user exits the app, create another local notification that fires when current stand time ends
Daniel Santos
34,969 PointsDaniel Santos
34,969 PointsI see. Let me see if I get the idea. Before the app goes to background, I will create this notification which will activate on
now + timeLeftInClock. When the user opens up the app, I can just update my properties with the time elapsed since the last saved time stamp. In the case that the user opens the app when the notification tells then to do so, there is nothing much I have to change (time is up). I just have to make sure to record the activity and report it to the Health Kit etc. From this idea, I think I can make it work. Does this portrait what you meant?Pasan Premaratne
Treehouse TeacherPasan Premaratne
Treehouse TeacherCorrect!
Daniel Santos
34,969 PointsDaniel Santos
34,969 PointsThank you for the help!