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
697
votes
24 answers

@selector() in Swift?

I'm trying to create an NSTimer in Swift but I'm having some trouble. NSTimer(timeInterval: 1, target: self, selector: test(), userInfo: nil, repeats: true) test() is a function in the same class. I get an error in the editor: Could not find an…
Arbitur
  • 38,684
  • 22
  • 91
  • 128
341
votes
6 answers

How do I use NSTimer?

How do I use an NSTimer? Can anyone give me step by step instructions?
lab12
  • 6,400
  • 21
  • 68
  • 106
291
votes
17 answers

How can I use Timer (formerly NSTimer) in Swift?

I tried var timer = NSTimer() timer(timeInterval: 0.01, target: self, selector: update, userInfo: nil, repeats: false) But, I got an error saying '(timeInterval: $T1, target: ViewController, selector: () -> (), userInfo: NilType, repeats: Bool) ->…
user3225917
  • 2,991
  • 2
  • 13
  • 17
162
votes
9 answers

How to run a method every X seconds

I'm developing an Android 2.3.3 application and I need to run a method every X seconds. In iOS, I have NSTimer, but in Android I don't know what to use. Someone have recommend me Handler; another recommend me AlarmManager but I don't know which…
VansFannel
  • 45,055
  • 107
  • 359
  • 626
94
votes
13 answers

Determine if UIView is visible to the user?

is it possible to determine whether my UIView is visible to the user or not? My View is added as subview several times into a Tab Bar Controller. Each instance of this view has a NSTimer that updates the view. However I don't want to update a view…
jantimon
  • 36,840
  • 23
  • 122
  • 185
87
votes
8 answers

UIScrollView pauses NSTimer until scrolling finishes

While a UIScrollView (or a derived class thereof) is scrolling, it seems like all the NSTimers that are running get paused until the scroll is finished. Is there a way to get around this? Threads? A priority setting? Anything?
mcccclean
  • 7,721
  • 10
  • 32
  • 36
81
votes
10 answers

What's the best way to detect when the app is entering the background for my view?

I have a view controller that uses an NSTimer to execute some code. What's the best way to detect when the app is going to the background so I can pause the timer?
jfisk
  • 6,125
  • 20
  • 77
  • 113
76
votes
10 answers

How do I create a NSTimer on a background thread?

I have a task that needs to be performed every 1 second. Currently I have an NSTimer firing repeatedly every 1 sec. How do I have the timer fire in a background thread (non UI-thread)? I could have the NSTimer fire on the main thread then use…
AWF4vk
  • 5,810
  • 3
  • 37
  • 70
64
votes
6 answers

Passing parameters to the method called by a NSTimer

How can I pass a parameter to the method called by a NSTimer? My timer looks like this: [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(updateBusLocation) userInfo:nil repeats:YES]; and I want to be able to pass a string to…
bubster
  • 887
  • 1
  • 10
  • 17
62
votes
21 answers

How can I make a countdown with NSTimer?

How can I make a countdown with an NSTimer using Swift?
Giovanie Rodz
  • 1,741
  • 3
  • 12
  • 7
59
votes
11 answers

Scheduled NSTimer when app is in background?

How do people deal with a scheduled NSTimer when an app is in the background? Let's say I update something in my app every hour. updateTimer = [NSTimer scheduledTimerWithTimeInterval:60.0*60.0 target:self selector:@selector(updateStuff)…
cannyboy
  • 24,180
  • 40
  • 146
  • 252
59
votes
4 answers

Getting Current Time in string in Custom format in objective c

I want current time in following format in a string. dd-mm-yyyy HH:MM How?
sagarkothari
  • 24,520
  • 50
  • 165
  • 235
58
votes
10 answers

Weak Reference to NSTimer Target To Prevent Retain Cycle

I'm using an NSTimer like this: timer = [NSTimer scheduledTimerWithTimeInterval:30.0f target:self selector:@selector(tick) userInfo:nil repeats:YES]; Of course, NSTimer retains the target which creates a retain cycle. Furthermore, self isn't a…
bendytree
  • 13,095
  • 11
  • 75
  • 91
55
votes
5 answers

How to trigger NSTimer right away?

I need to call a method when my app starts and then call the same method every 5 seconds. My code is pretty simple: // Call the method right away [self updatestuff:nil] // Set up a timer so the method is called every 5 seconds timer = [NSTimer…
Luc
  • 16,604
  • 34
  • 121
  • 183
50
votes
2 answers

How to stop NStimer event?

Possible Duplicate: NSTimer doesn't stop In my application I am using NStimer to call an animation function every 3 seconds. I want to stop this timer and called another event while the timer is still running. Is this possible?
Mansi
1
2 3
99 100