- (void) handleTimer: (NSTimer *) timer
{
do some work here...
} // handleTimer
NSTimer *timer;
timer = [NSTimer scheduledTimerWithTimeInterval: 0.5
target: self
selector: @selector(handleTimer:)
userInfo: nil
repeats: YES];
applicationShouldTerminate: message, you're likely to want to returnNSTerminateLater so that you can first gracefully shut down the connection before quitting.
There is a gotcha: returning NSTerminateLater stops the main runloop, so your NSTimers will stop working from that point on. Thus, if you were depending on a timer firing to finish up your shut-down process, you'll never see it, and you'll hang. The solution is to return NSTerminateCancel, then do whatever you need to do and then terminate yourself manually. (Thanks to Larry Gerndt for this one!)
var sessionTimer: NSTimer?
...
sessionTimer =
NSTimer.scheduledTimerWithTimeInterval(0.5, target: self,
selector: #selector(SessionEditorViewController.lubDub(_:)),
userInfo: nil, repeats: true)
...
func lubDub(timer: NSTimer) {
let elapsedTime = NSDate().timeIntervalSince1970 - startTime
print("(elapsedTime)")
}
...
// clean up when done
sessionTimer?.invalidate()
sessionTimer = nil