Ad

Update React [Native] View On Day Change

I'm working on a React Native component that should update itself when the day changes, even when the user hasn't interacted with the view.

Is setInterval the only way to deal with this? If I call setTimeout in viewDidLoad specifying the number of ms until the next day, won't the interval be inaccurate if the app gets paused? Alternatively, I could run a periodic timer, but I'd need to give it a short interval so that there's not an apparent delay when the day changes -- seems pretty inefficient.

Am I missing something?

Ad

Answer

I suppose this is more of a general app architectural concern. The simplest answer is you should take the current time in seconds (new Date().getTime()), determine how many seconds there are until the next day, and set a timer for that number.

However, as you mentioned, once the app is killed, that timer will no longer be accurate.

You should use AppStateIOS to listen for when the app is backgrounded and foregrounded. Upon being sent to the background, clear the timer. Upon being brought to the foreground, run the original calculation again and generate a new timer.

Unfortunately there is no equivalent functionality on Android as of yet.

Ad
source: stackoverflow.com
Ad