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
5
votes
2 answers

NSTimer callbacks while iPhone application is inactive

This question seems to be the essence of several others on this forum. I believe that it is possible for the active iPhone application to continue running, and specifically, to continue receiving timer call-backs, after it has entered the inactive…
Benjohn Barnes
5
votes
3 answers

How many NSTimers is too many?

In an iPhone OS device, how many simultaneously running NSTimers would be too many? I have a bunch of routines being called by a single timer firing 25 times a sec, and things are pretty choppy, slow, and heavy. I am thinking about adding one or…
RexOnRoids
  • 14,002
  • 33
  • 96
  • 136
5
votes
3 answers

iOS 5 deep sleep prevention

I'm trying to build an alarm app that can fire an alarm while in locked-screen mode (the app is in the foreground, but the screen is locked). The alarm has to be triggered by a NSTimer not by uilocalnotification. In iOS 4 I used the 'play silent…
Peter Sarnowski
  • 11,900
  • 5
  • 36
  • 33
5
votes
4 answers

Why does Xcode 4 mark variables as unused even if they are?

I'm instantiating and scheduling a timer variable but Xcode compiler and analyzer marks my var "levelScoreTimer" with 2 warnings like "warning: unused variable 'levelScoreTimer' and "Dead store: Value stored to 'levelScoreTimer' during its…
Centurion
  • 14,106
  • 31
  • 105
  • 197
5
votes
1 answer

AVPlayerStatus vs AVPlayerItemStatus

The issue is that player.status returns AVPlayerStatusReadyToPlay a full 2 seconds before player.currentItem.status returns AVPlayerItemStatusReadyToPlay. Does anyone have any helpful explanations as to why this is happening? This is just sample…
SteveB
  • 652
  • 3
  • 12
5
votes
2 answers

Would NSTImer events block the main thread?

When we use a NSTimer, once the call back is called after the mentioned interval, does the UI would be blocked?
RK-
  • 12,099
  • 23
  • 89
  • 155
5
votes
3 answers

Swift 4 Timer Crashes with NSException

I've been searching for a way to use a Timer in Swift 4 and looked at this article. I test out my code in xcode and when the the timer first ticks (in this case after 10 seconds) the app crashes and I get an error, although the build…
Branson Camp
  • 641
  • 1
  • 9
  • 18
5
votes
1 answer

Problem using NSTimer in MonoTouch .NET thread

I have a problem using an NSTimer in MonoTouch. To start with I ran an NSTimer in my main thread, which worked. However, I've moved the timer to a separate thread, and I never get a callback. I'm guessing this is because my .NET style thread isn't a…
vlad259
  • 1,236
  • 10
  • 24
5
votes
8 answers

NSTimer as a self-targeting ivar

I have come across an awkward situation where I would like to have a class with an NSTimer instance variable that repeatedly calls a method of the class as long as the class is alive. For illustration purposes, it might look like this: //…
Matt Wilding
  • 20,115
  • 3
  • 67
  • 95
5
votes
1 answer

Difference between weak references in a block and a NSTimer

As we know we need to use a weak reference inside a block to break the retain cycle, like so: __weak id weakSelf = self; [self doSomethingWithABlock:^() { [weakSelf doAnotherThing]; }] However weak references can not break the retain cycle…
dark_chenshifei
  • 213
  • 2
  • 9
5
votes
1 answer

Trivial "+[NSTimer scheduledTimerWithTimeInterval:repeats:block:]: unrecognized selector" error

I am banging my head against an odd error after a move to 10.12/Sierra and Xcode 8.1: +[NSTimer scheduledTimerWithTimeInterval:repeats:block:]: unrecognized selector sent to class 0x7fff78f1fa88 The most minimal code (default settings of…
Dirk-Willem van Gulik
  • 7,566
  • 2
  • 35
  • 40
5
votes
2 answers

NSTimer for UITableviewcell similar to World Clock app

I would like to build a few simple countdown timers in a UITableview. I noticed the World Clock app has animated clocks in each of it's rows. What is the best method to firing an NSTimer to update each table row? Can a single timer be run that…
SonnyBurnette
  • 680
  • 3
  • 11
  • 25
5
votes
1 answer

Strictly scheduled loop timing in Swift

What is the best way to schedule a repeated task with very strict timing (accurate and reliable enough for musical sequencing)? From the Apple docs, it is clear that NSTimer is not reliable in this sense (i.e., "A timer is not a real-time…
c_booth
  • 2,185
  • 1
  • 13
  • 22
5
votes
2 answers

Adding and Subtracting times in Swift

I've written some of this in pseudo code because I don't know the syntax for it. I'd like to have the timeLeftLabel.text reflect how many hours, minutes, and seconds are left until the 6 hours are up. My biggest problem is that I don't know how to…
adman
  • 408
  • 1
  • 4
  • 21
5
votes
2 answers

Calling a NSTimer method

I'm new to working with timers on the iPhone and would need some support. I have a method as below that takes the time and update a UILabel in my userinterface. I also have an NSTimer that calls that method once a second (I only show hours and…
Structurer
  • 694
  • 1
  • 10
  • 23