Is it possible to schedule a timer in a notification service extension?

I'm trying to fire a timer in a notification service extension, but what would work in the app doesn't work in the extension i.e.

        Timer.scheduledTimer(timeInterval: 8.0, target: self, selector: #selector(doSomeStuff2), userInfo: nil, repeats: false)
        
or 
        Timer.scheduledTimer(withTimeInterval: 7.0, repeats: false) { timer in
            ...
        }

and so on, etc.

Is that a way of getting a timer to fire in an extension?

This is almost certainly a run loop issue. A timer created this way is scheduled in the default run loop mode of the current thread’s run loop. In app code, that’s typically the main thread and everything works. In extension code, you may be running off the main thread or the main thread may not be running its run loop.

The solution depends on the nature of the problem:

  • If you’re running on a secondary thread, try bouncing to the main thread using -performSelectorOnMainThread:….

  • If the extension doesn’t run its run loop, you can start your own thread to do that.

  • But in that case it’s probably easiest to use a Dispatch timer source scheduled on the main queue.

For more background on this, see WWDC 2010 Session 207 Run Loops Section.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Is it possible to schedule a timer in a notification service extension?
 
 
Q