I'm trying to execute a snippet of code (Swift 4.2) once in 24 hours. The following complies but only works once! This implies that it doesn't make it into @objc func calendarDayDidChange()
which means nothing is being fetched.
Do I need to define this notification in my view controller before calling it (as show below) in the viewDidLoad()?
I've looked at some other threads such as
- NotificationCenter registered selector is not called
- How to use NSCalendarDayChanged - swift
- Run code at midnight everyday
- Checking when a date has passed - Swift
but that didn't seem to work for me either.
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self,
selector:#selector(calendarDayDidChange),
name:.NSCalendarDayChanged,
object:nil)
}
@objc func calendarDayDidChange()
{
// do something at once a day
fetchThingsFromFirebase()
}