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

How to get an NSTimer to stop repeating after a condition is met

I have two NSTimers that I programmed to make a button appear on the screen and then disappear. How can I program it to stop adding and removing buttons once a condition is met? Here is my code: var timerRemoveButton =…
bhzag
  • 2,932
  • 7
  • 23
  • 39
10
votes
2 answers

NSTimer Reliable Alternatives

I have an application that is supposed to log certain things every 1 second and I'm currently using NSTimer, but if my application transitions screens (or almost anything else, really) it slows down the timer a little bit making for inaccurate…
Baub
  • 5,004
  • 14
  • 56
  • 99
10
votes
2 answers

scheduledTimerWithTimeInterval vs performselector with delay with iOS 5.0

i am doing function call with scheduledTimerWithTimeInterval. i am just checking that xml parsing is completed or not for particular web services and invalidating timer in didEndElement method after getting successful…
Paras Gandhi
  • 823
  • 1
  • 13
  • 28
10
votes
3 answers

PerformSelector After delay doesn't run in background mode - iPhone

I have a voip application which runs constantly on the background as well. While I'm in the background I'm calling from the main thread: (to establish network connection in case I diagnose a network lost). [self performSelector…
Idan
  • 5,717
  • 10
  • 47
  • 84
10
votes
3 answers

NSTimer not firing

I have an NSTimer that I init with this code: testTimer = [[NSTimer alloc] initWithFireDate:[new objectAtIndex:0] interval:0.0 target:self selector:@selector(works:) userInfo:nil repeats:NO]; [new objectAtIndex:0] is an NSDate in the past. When I…
Matt S.
  • 13,305
  • 15
  • 73
  • 129
10
votes
2 answers

Repeating NSTimer, weak reference, owning reference or iVar?

I thought I would put this out here as a separate question from my previous retaining-repeating-nstimer-for-later-access as the discussion has moved forward making a new question clearer than yet another EDIT: The scenario is an object creates a…
fuzzygoat
  • 26,573
  • 48
  • 165
  • 294
10
votes
1 answer

NSTimer is not firing when called in a block

This works func startTimer () { batchTimer = NSTimer.scheduledTimerWithTimeInterval(batchIntervalSeconds, target: self, selector: #selector(Requester.performRemoteRequests), userInfo: nil, repeats: false) } This doesn't func startTimerInBlock…
Daniel K
  • 1,119
  • 10
  • 20
10
votes
1 answer

NSTimer doesn't find selector

I want to use an NSTimer in a class that doesn't inherit from UIViewVontroller. I have 2 files : a ViewController and a TimerClass like that : ViewController: import UIKit class ViewController: UIViewController { var timerClass =…
soling
  • 541
  • 6
  • 20
10
votes
5 answers

Having trouble with NSTimer (Swift)

--EDITED WITH UPDATED INFORMATION-- What I wish to do is call a function named timerFunc once every five seconds using a NSTimer.scheduledTimerWithTimeInterval method, the issue seems is that during runtime, I get the error Terminating app due to…
Althonos
  • 131
  • 1
  • 9
10
votes
2 answers

NSTimer not firing selector when added with scheduledTimerWithTimeInterval

I have a code snippet like this: m_timer = [NSTimer scheduledTimerWithTimeInterval:timeOutInSeconds target:self …
xceph
  • 1,036
  • 2
  • 13
  • 28
10
votes
1 answer

How to run NSTimer in background beyond 180sec in iOS 7?

I have tried this but not working more than 180 sec in iOS 7 and Xcode 4.6.2. Please help me UIBackgroundTaskIdentifier bgTask = UIBackgroundTaskInvalid; UIApplication *app = [UIApplication sharedApplication]; bgTask = [app…
Deepak Gupta
  • 979
  • 1
  • 10
  • 18
10
votes
2 answers

Writing a unit test to verify NSTimer was started

I'm using XCTest and OCMock to write unit tests for an iOS app, and I need direction on how to best design a unit test that verifies that a method results in an NSTimer being started. Code under test: - (void)start { ... self.timer =…
Richard Shin
  • 663
  • 5
  • 17
10
votes
4 answers

NSTimer not calling Method

I have an NSTimer that should call a method every second, but it doesn't seem to be calling it at all. This is how I declare the timer: [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(fadeManager:) userInfo:nil repeats:YES]; This…
sinθ
  • 11,093
  • 25
  • 85
  • 121
10
votes
3 answers

how to show countdown on uilabel in iphone?

i have a uilabel and a uislider on a view. i want to set label's time using slider.range of slider is 00:00:00 to 03:00:00. means 3 hours.and intervel on slider is 0.5 minutes.also how to show.i want the timer runs even if application is closed.
Rahul Vyas
  • 28,260
  • 49
  • 182
  • 256
10
votes
3 answers

iPhone dev -- performSelector:withObject:afterDelay or NSTimer?

To repeat a method call (or message send, I guess the appropriate term is) every x seconds, is it better to use an NSTimer (NSTimer's scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:) or to have the method recursively call itself at…
mk12
  • 25,873
  • 32
  • 98
  • 137