Questions tagged [nstimer]

An Objective C class for creating timer objects.

In Objective C (), you use the NSTimer class to create timer objects or, more simply, timers. A timer waits until a certain time interval has elapsed and then fires, sending a specified message to a target object. For example, you could create an NSTimer object that sends a message to a window, telling it to update itself after a certain time interval.

Timers work in conjunction with run loops. To use a timer effectively, you should be aware of how run loops operate—see NSRunLoop and Threading Programming Guide. Note in particular that run loops retain their timers, so you can release a timer after you have added it to a run loop.

A timer is not a real-time mechanism; it fires only when one of the run loop modes to which the timer has been added is running and able to check if the timer’s firing time has passed. Because of the various input sources a typical run loop manages, the effective resolution of the time interval for a timer is limited to on the order of 50-100 milliseconds. If a timer’s firing time occurs during a long callout or while the run loop is in a mode that is not monitoring the timer, the timer does not fire until the next time the run loop checks the timer. Therefore, the actual time at which the timer fires potentially can be a significant period of time after the scheduled firing time.

NSTimer is “toll-free bridged” with its Core Foundation () counterpart, CFRunLoopTimerRef. See “Toll-Free Bridging” for more information on toll-free bridging.

Additional examples for the NSTimer object can be found here.

2624 questions
8
votes
4 answers

Stop and restart a timer

I want to stop this timer and then restart it from where I stopped it. secondsTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(addSeconds), userInfo: nil, repeats: true) Below, it was suggested I shouldn't…
Pietro P.
  • 185
  • 1
  • 1
  • 7
8
votes
2 answers

Deinit / Invalidate Timer

I'm trying to deinit/invalidate Timer when user press back button but not when he push to next ViewController. var timer = Timer() timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(timePrinter),…
Nitesh
  • 1,564
  • 2
  • 26
  • 53
8
votes
1 answer

Background task stopping when device locked?

I have a timer running when the device enters the background as I want to keep a check on a small amount of data in my service. I am using the following code in the applicationDidEnterBackground method in app delegate UIApplication *app =…
Tony Law
  • 293
  • 2
  • 13
8
votes
1 answer

Calling private functions with NSTimer?

Is there a way to have an NSTimer call a private func when the timeout has expired? (Does this even make sense? The NSTimer reference is local to the class, I'm not sure the caller's target at runtime is local, as well.)
Joe
  • 115
  • 5
8
votes
2 answers

How to update UITableViewCells using NSTimer and NSNotificationCentre in Swift

NOTE: Asking for answers in Swift please. What I'm trying to do: Have tableview cells update every 1 second and display a real-time countdown. How I'm doing it currently: I have a tableview with cells that contain a label. When the cells are…
8
votes
2 answers

Validate a NSTimer that was invalidated

How can I recall or reestablish a timer that was previously invalidated? I am trying to allow the user to revalidate/restart a timer when they click a button. Is this possible?
B1GR0BB1E
  • 85
  • 1
  • 5
8
votes
3 answers

Stop / Pause swift app for period of time

My app uses multiple threads of NSTimer Object. Within the one function (called randomly at random times, not a fixed delay) I want it to pause the whole app, pause the threads for 1 second only. I have the following code: [self…
user3834266
8
votes
2 answers

Should a NSTimer always be added into a runloop to execute

I didn't explicitly add timers to a runloop and it works just fine. The other day when I read some article about NSRunLoop and it said it's better to add a NSTimer instance into a runloop instance to execute. I just wonder will it do any harm if I…
Xin Yuan
  • 255
  • 1
  • 8
8
votes
5 answers

Reset an NSTimer's firing time to be from now instead of the last fire

I have a NSTimer that fires with an interval of 3 seconds to decrease a value. When I do an action that increases that value, I want to restart the timer to count 3 seconds from that point. For example, if I increase the value and timer will fires…
emenegro
  • 6,901
  • 10
  • 45
  • 68
8
votes
1 answer

IOS receiving data timeout CFsocket

I want to connect with pool.ntp.org to time sync. So am creating a socket sock=CFSocketCreate(NULL, PF_INET, SOCK_DGRAM, IPPROTO_UDP, kCFSocketDataCallBack|kCFSocketWriteCallBack|kCFSocketConnectCallBack, sockCallback, &sock_ctx); then am…
tspentzas
  • 178
  • 1
  • 10
7
votes
1 answer

NSTimer is it thread safe?

I have a repeated timer with interval of 1/4 second. I am initializing it like this: [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(toggleCams) …
0xSina
  • 20,973
  • 34
  • 136
  • 253
7
votes
3 answers

Observer pattern for stopwatch

I'm trying to implement a stopwatch based on the MVC model. The stopwatch uses the NSTimer with the selector -(void) tick being called every timeout. I've tried to make the stopwatch as a model for reusability but I've run into some design problems…
7
votes
3 answers

Set an NSTimer to fire once in the future

How do I set up an NSTimer to fire once in the future (say, 30 seconds). So far, I have only managed to set it so it fires immediately, and then at intervals.
cannyboy
  • 24,180
  • 40
  • 146
  • 252
7
votes
3 answers

UIScrollView pauses NSTimer while scrolling

I have a UIScrollView that has a series of labels which are rapidly updating numbers (every .06 seconds). While the scroll view is moving, however, the NSTimer is paused and does not continue until after the scrolling and the elastic animation have…
Peter Kazazes
  • 3,600
  • 7
  • 31
  • 60
7
votes
2 answers

How can I update my NSTimer as I change the value of the time interval

I have an NSTtimer implemented and it's working fine. I also have the time interval argument connected to a UISlider for the iPhone. However when I change it's value the NSTimer is still running at the original time interval it does not get updated.…
cgossain
  • 199
  • 2
  • 6