I want to create a reminder app. There should be a DB with table of 'active-reminders'. There would be a function which will:
I was thinking about an efficient way to run this function and I thought about:
My question: Is there a better way to implement the solution? From the methods listed, would you say one of the ways is more efficient that the other one?
Many Thanks!
If your appointments are in memory, sort them by trigger time. If your appointments are in a database, make sure there's an efficient mechanism for accessing the next appointment to reach its time.
Then, using this efficient sorted access mechanism, find the earliest appointment to fire. If that time has already passed, then fire its notification, remove it from the data structure and repeat. If that time has not yet passed, set a timer for it's scheduled time. When that timer fires remove that event from the data structure and repeat the process by getting the next event to fire and setting a timer for it.
Whenever you add a new appointment, check if it's due time is earlier than the one you have a timer for. If so, release the timer for the one you had and set the timer for the new one.
Whenever an appointment is to be deleted, you first check if it's the next one to notified. If so, stop its timer and configure the next event.
You can likely do all this with four functions that handle the life cycle changes:
addNewEvent(event) // called when you add a new event to the data
fireEvent(event) // called when a timer fires to notify of an event
removeEvent(event) // called when an event is being deleted
configureNextEvent() // called to configure timer for next event