1

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

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()

    }
  • Have you tried `func calendarDayDidChange(notification: NSNotification)`? – Callam Jan 31 '19 at 19:47
  • Yes, while that complies to my its still not going into the code block of @objc func calendarDayDidChange(notification : NSNotification) so it can fetchThingsFromFirebase() – swiftEnthusiast Jan 31 '19 at 19:56
  • change your calendarDayDidChange declaration from `@objc func calendarDayDidChange()` to `@ojbc func calendarDayDidChange(_ notification: Notification)` – Leo Dabus Feb 01 '19 at 01:19
  • Discussion If the the device is asleep when the day changes, this notification will be posted on wakeup. Only one notification will be posted on wakeup if the device has been asleep for multiple days. There are no guarantees about the timeliness of when this notification will be received by observers. As such, you should not rely on this notification being posted or received at any precise time. – Leo Dabus Feb 01 '19 at 01:25

0 Answers0