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
10
votes
1 answer

Is it safe to schedule and invalidate NSTimers on a GCD serial queue?

What's the right way to do this? The NSTimer documentation says this: Special Considerations You must send this message from the thread on which the timer was installed. If you send this message from another thread, the input source associated…
9
votes
2 answers

NSTimer Xcode Warning

I have an NSTimer that I initialize with scheduledTimerWithTimeInterval: with a very short interval (.1 seconds) with no repeat, and then never use it again as it invalidates itself and therefore releases its retain on the target. Xcode warns that…
Jesse Naugher
  • 9,780
  • 1
  • 41
  • 56
9
votes
5 answers

NSTimer doesn't call method

I'm really frustrated now, googled the whole internet, stumbled through SO and still didn't find a solution. I'm trying to implement an NSTimer, but the method which I defined doesn't get called. (seconds are set correctly, checked it with…
tamasgal
  • 24,826
  • 18
  • 96
  • 135
9
votes
3 answers

NSTimer disables dealloc in UIView

@interface someview:UIView{ NSTimer* timer; } @end @implementation someview -(void)dealloc{ NSLog(@"dealloc someview"); [timer invalidate]; timer = nil; } -(void)runTimer{ // } -(void)someMethod{ timer = [NSTimer…
ssj
  • 881
  • 1
  • 10
  • 21
9
votes
1 answer

NSTimer never starts

I'm just trying to close an NSPanel after a couple second delay, but I can't get my NSTimer to start. It will fire if I explicitly call the fire method on it, but it will never go by itself. Here's my code: -…
strange quark
  • 5,205
  • 7
  • 41
  • 53
9
votes
2 answers

Timer does not run in Swift 3.0 playground

Working in playground with Swift 3.0 I have this code: struct Test { func run() { var timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: false) { timer in print("pop") } } } let test =…
Mercutio
  • 1,152
  • 1
  • 14
  • 33
9
votes
1 answer

How to reset NSTimer? swift code

So I'm creating an app/game where you tap a button corresponding to a picture before the timer runs out and you lose. You get 1 second to tap the button, and if you choose the right button then the timer resets and a new picture comes up. I'm having…
LuKenneth
  • 2,197
  • 2
  • 18
  • 41
9
votes
2 answers

iPhone: NSTimer Countdown (Display Minutes:Seconds)

I have my timer code set up, and it's all kosher, but I want my label to display "Minutes : seconds" instead of just seconds. -(void)countDown{ time -= 1; theTimer.text = [NSString stringWithFormat:@"%i", time]; if(time == 0) { …
user298261
  • 422
  • 3
  • 17
9
votes
2 answers

Timer in another thread in Objective - C

I have to define method which should be invoked periodically with some time interval. I need to invoke it in another thread (NOT main thread), because this method is used to get information from external API and sync data in core data. How do I…
user2375706
  • 705
  • 2
  • 8
  • 14
9
votes
7 answers

NSTimer Not Stopping When Invalidated

I have the following code in my .h file: #import #import #import #import @interface LandingController : UIViewController { …
NCoder
  • 325
  • 3
  • 10
  • 25
9
votes
5 answers

How to stop/invalidate NStimer

I am using [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(update) userInfo:nil …
Shishir.bobby
  • 10,994
  • 21
  • 71
  • 100
9
votes
1 answer

How to update a subscription with StoreKit?

I'm trying to work out the best mechanism to handle the auto renewing part so that it handles the continuation of the subscription into the next period. What the best way of handling this? Should I have an NSTimer set to check if the current…
cannyboy
  • 24,180
  • 40
  • 146
  • 252
9
votes
5 answers

NSTimer scheduledTimerWithTimeInterval:target:selector:userInfo:repeats doesn't invoke the method

The timer never invokes the method. What am I doing wrong ? This is the code: NSTimer *manualOverlayTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(hideManual) userInfo:nil…
aneuryzm
  • 63,052
  • 100
  • 273
  • 488
9
votes
2 answers

NSTimer periodic task doesn't get called while scrolling

I have an NSTimer timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(periodicTimer) userInfo:nil…
Fr4ncis
  • 1,387
  • 1
  • 11
  • 23
8
votes
3 answers

How keep NSTimer when application entering background?

I'm here because a didn't find any solutions for my issue :( I'm doing an simple application in which i have to send (by socket) some informations to a server (like GPS l/L, accuracy, Battery level, etc). The current code works fine when application…
UIChris
  • 601
  • 2
  • 6
  • 19