How to update NSMenuItem while NSMenu is showing in macOS

Issue #213

Use Runloop

timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { [weak self] _ in
    let date = Date()
    self?.updateStopItem(seconds: finishDate.timeIntervalSince1970 - date.timeIntervalSince1970)
})
        
RunLoop.main.add(timer!, forMode: .common)

Use Dispatch

timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { [weak self] _ in
    let date = Date()
    DispatchQueue.main.async {
        self?.updateStopItem(seconds: finishDate.timeIntervalSince1970 - date.timeIntervalSince1970)
    }
})

Updated at 2020-09-08 07:54:36

Written by

I’m open source contributor, writer, speaker and product maker.

Start the conversation