I am currently writing an IOS app that uses Cloud Firestore as a database. One of the app's functions is a ticking down timer that needs to be shown on the screen for users to show how much time is left for a function. Problem is I am currently using a timer, but that doesn't work as when I exit the app it will stop updating the database on how much time is left . Any suggestions on how I could solve this problem?
-
This could be pretty easily done with a cron job and Firebase Cloud functions. See this [Cloud functions in Firebase](https://stackoverflow.com/questions/42790735/cloud-functions-for-firebase-trigger-on-time) for some excellent info which should provide a solution. – Jay Dec 22 '18 at 17:09
-
I would also think you would be comparing the actual time (either retrieved from the Firebase server or local app time) to the end time stored in Firebase. So regardless of when they left the app, if they log back in it would be expired or show them the time left. Of course the downside to using a locally obtained time from the device is that it could be changed by the user, throwing off the actual calculation. It would be best to pull the server time when the app is logged in and use that for comparison. – Jay Dec 22 '18 at 17:13
2 Answers
There's no way you would be able to interact with your code while your app is closed. iOS is strict, once your app is closed, you have 30 seconds to process anything necessary. your app is suspended from regular activities,
As @matt mentioned:
A timer can run in the background only if both the following are true:
Your app for some other reason runs in the background. (Most apps don't; most apps are suspended when they go into the background.) And: The timer was running already when the app went into the background.
I also suggest a look over here: Swift 3 - How to make timer work in background

- 2,338
- 1
- 14
- 30
-
Does that mean there is no way for me to update the timer on the server side? Does firebase offer any customisation options in that regard? So like I would send a request from the iOS app and then the database would tick down automatically – Marco Cappai Dec 22 '18 at 08:36
-
You can’t, you could save the tick count in UserDefaults and then if by any chance the app is opened again sync your database with the new data – excitedmicrobe Dec 22 '18 at 08:38
-
Oh okay, thanks. In alternative is there any way for me to tell the server to perform a command after an amount of time and that performs even when I'm not using the app. For example I press a button on one user and something gets performed for another user an hour later. – Marco Cappai Dec 22 '18 at 08:45
-
Don’t do that. That’s bad. You don’t need to mix user data. This will eat up your Firestore database – excitedmicrobe Dec 22 '18 at 08:46
-
What do you mean? I meant like for example I send a friend request but it only shows up for another user an hour later. What would be a solution for that? – Marco Cappai Dec 22 '18 at 08:48
-
-
1
Set a date and time in Database when the function for Timer is called. You can then check whenever the app is open to calculate the amount of time taken.

- 1,738
- 1
- 19
- 25