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

NSTimer not fired when uiscrollview event occurs

I have a UIImageView placed in UIScrollView, Basicly this UIImageView holds very big map, and created animation on a predefined path with "arrows" pointed navigation direction. But, whenever uiscrollevents occurs, I think MainLoop freezes and…
ubaltaci
  • 1,016
  • 1
  • 16
  • 27
18
votes
2 answers

Creating an iOS Timer

I am trying to create a "stop watch" type functionality. I have one label (to display the elapsed time) and two buttons (start and stop the timer). The start and stop buttons call the startTimer and stopTimer functions respectively. Every second the…
v0idless
  • 948
  • 2
  • 11
  • 24
18
votes
2 answers

Is NSTimer expected to fire when app is backgrounded?

I don't understand it at all but NSTimer in my app definitely is running in background. I have a NSLog in method run by the timer and it is logging while it's in background. It's on iPhone 4 with iOS 4.2.1. I have declared location background…
JakubM
  • 2,685
  • 2
  • 23
  • 33
18
votes
4 answers

NSTimer not working while dragging a UITableView

I have an application with a countdown timer. I've made it with a label that is updated with a function called from a timer this way: ... int timeCount = 300; // Time in seconds ... NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:1…
rai212
  • 711
  • 1
  • 6
  • 20
17
votes
5 answers

How to Move Timer to Background Thread

I currently have two NSTimer timers in my app that are responsible for taking data and updating the UI. I have noticed recently that while the timers are running I have poor UI performance, for example the user can't scroll through my UITableview…
user3185748
  • 2,478
  • 8
  • 27
  • 43
15
votes
5 answers

How to detect a pause in input for UISearchBar/UITextField?

I have the following UISearchbar code: - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; NSString* endpoint =[NSString…
Sheehan Alam
  • 60,111
  • 124
  • 355
  • 556
15
votes
8 answers

iPhone - NSTimer not repeating after fire

I am creating and firing a NSTimer with: ncTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(handleTimer:) …
Zigglzworth
  • 6,645
  • 9
  • 68
  • 107
15
votes
2 answers

What is replacement of NSTimer.scheduledTimerWithTimeInterval() in swift 3?

I want to implement NSTimer.scheduledTimerWithTimeInterval in Swift 3. I want to know the Replacement of NSTimer.scheduledTimerWithTimeInterval(0.35, target: self, selector: #selector(createEnemy), userInfo: nil, repeats: true) as we used it in…
Mohsin Qureshi
  • 1,203
  • 2
  • 16
  • 26
15
votes
3 answers

swift NSTimer userinfo

I'm trying to pass a UIButton with a NSTimer's userinfo. I've read every post on stackoverflow on NSTimers. I'm getting very close but can't quite get there. This post has helped Swift NSTimer retrieving userInfo as CGPoint func…
user2164327
  • 283
  • 1
  • 2
  • 13
14
votes
6 answers

How to Pause/Play NSTimer?

I need to start NSTimer by 0:0:0 that I am doing and I have a Pause button on click of that the timer has to pause after some time if a user clicks play it has to run from paused value of time. How can I pause and play in NSTimer? Any help? Thanks…
taus-iDeveloper
  • 681
  • 2
  • 8
  • 22
14
votes
5 answers

How to use Swift Selector? Selector constantly results to `Type has no member`

I'm struggling with the following piece of code: backgroundTimer = NSTimer.scheduledTimerWithTimeInterval(3, target: gvc, selector: #selector(GameViewController.addNext), userInfo: nil, repeats: true) I can't seem to get the #selector working. This…
user594883
  • 1,329
  • 2
  • 17
  • 36
14
votes
3 answers

What is difference between self.timer = nil vs [self.timer invalidate] in iOS?

Can anyone explain me self.timer=nil vs [self.timer invalidate]? What exactly happens at the memory location of self.timer? In my code self.timer=nil doesn't stops the timer but [self.timer invalidate] stops the timer. If you require my code I…
Nasir
  • 1,617
  • 2
  • 19
  • 34
14
votes
4 answers

NSTimer memory management

When I execute this code: [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(showButtons) userInfo:nil repeats:NO]; do I need to nil it or release it, ot whatever for memory management? I am using ARC
Alessandro
  • 4,000
  • 12
  • 63
  • 131
14
votes
2 answers

Add a running countup display timer to an iOS app, like the Clock stopwatch?

I'm working with an app that processes device motion events and updates interface in 5 second increments. I would like to add an indicator to the app that would display the total time the app has been running. It seems that a stopwatch-like counter,…
Alex Stone
  • 46,408
  • 55
  • 231
  • 407
13
votes
1 answer

NSTimers running in background?

I was under the impression that NSTimer did not work at all after an application calls applicationWillResignActive. I seems however that existing NSTimers (i.e. ones created before the application resigned active) will continue to run and its only…
fuzzygoat
  • 26,573
  • 48
  • 165
  • 294