have a reference to an object inside NSTimer selector - ios

i have more than 10 objects of the same object type, lets call it Car type.
in my class, for each object i need to start a NSTimer.
//for object 1:
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(timerTick:) userInfo:nil repeats:YES];
//for object n:
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(timerTick:) userInfo:nil repeats:YES];
[EDIT] the timers are fired separately not at the same time.
However i have only one method for the 10 objects:
- (void)timerTick:(NSTimer *)timer
{
//here i need the reference to the object x (car) in order to display the timer and the name of the car for instance.
}
how can i achieve this, please?
im thinking of subclassing NSTimer and adding a new property (car) to it.
CustomTimer *mytimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(timerTick:) userInfo:nil repeats:YES];
- (void)timerTick:(CustomTimer *)timer
{
NSLog(#"%#", timer.car.name);
}
thanks

Related

WebService Call after timeInterval

I want to my Webservice invoke once every 6 Hours.I am newer in iOS. Please help any help would be apperciated.I am stuck.
You can user NSTimer and schedule it for 6 hours
NSTimer *timer = nil;
timer = [NSTimer scheduledTimerWithTimeInterval:6*60*60 //6 hour
target:self
selector:#selector(performAction)
userInfo:nil
repeats:YES];
you can call using NSTimer. this is automatically called which time you set.
e.g.
NSTimer *timer= [NSTimer scheduledTimerWithTimeInterval:10.0(your time) target:self selector:#selector(someMethod) userInfo:nil repeats:YES];
-(void)someMethod
{
////API called here...
}
In ViewDidLoad
NSTimer *timer = [NSTimer timerWithTimeInterval:360.0 target:self selector:#selector(hideandview) userInfo:nil repeats:YES];
-(void)hideandview
{
////API called here...
}
You should save the last time in prefence (NSUserDefaults) when you make a call.
Whenever the app starts. Start timer and check current time with last saved time and get the difference.

How do I see if NSTimers are stacking and how to prevent it?

Im making an iphone game that uses NStimer for movement. I found out that there is a bug that makes the timers stack some times, but i havnt found out what causes is. Is there any way to see how many timers are allocated and is there any way to prevent it by doing something like this:
If(myTimer.numberOfAllocatedTimers == 0) {
myTimer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:#selector(updateme) userInfo:nil repeats:YES];
}
Try this:
// use this to start timer
myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(doSthing) userInfo:nil repeats:YES];
// use this to stop timer
if (myTimer) {
[myTimer invalidate];
myTimer = nil;
}
A little more context on where this code is being used would be helpful.
However, I'll try to explain why it's happening anyway.
Every time you use [NSTimer scheduledTimerWithTimeInterval...], a new timer is created, and will start repeating. If your code creates a timer inside the callback method, see example below, you'll stack timers.
- (void)updateme {
if ([NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:#selector(updateme) userInfo:nil repeats:YES]) {
// This creates stacked timers!
}
}
Here's how you should do it:
#property (nonatomic, strong) NSTimer *myTimer;
...
self.myTimer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:#selector(updateme:) userInfo:nil repeats:YES];
...
- (void)updateme:(NSTimer *)timer {
if (timer == self.myTimer) {
// Do something because the timers are equal
}
}

performSelector in timeintervals

I have the following method
-(void) showDie: (int) num;{
// do something
}
I want to call that method in time intervals of .5 secs. So far I have the following
SEL runShowDie;
[runShowDie performSelector:#selector(showDie:) withObject:rand];
[NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:runShowDie userInfo:nil repeats:NO];
What am I doing wrong?
You put repeats:No in this line make it YES
[NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:#selector(showDie:) userInfo:[NSString stringWithFormat:#"%d",rand] repeats:YES];
Try with
[self performSelector:#selector(showDie:) withObject: rand afterDelay:0.5f];
And remove NSTimer

iOS NSTimer not working? [duplicate]

This question already has answers here:
NSTimer timerWithTimeInterval: not working
(4 answers)
Closed 8 years ago.
This is my exact code, and it doesn't seem to be working. Can you tell me what I am doing wrong? Note that refreshTimer was already declared in the private interface.
-(void)viewDidLoad {
refreshTimer = [NSTimer timerWithTimeInterval:1 target:self selector:#selector(timerTest) userInfo:nil repeats:YES];
}
-(void)timerTest {
NSLog(#"Timer Worked");
}
Give scheduledTimerWithTimeInterval a try:
NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(myMethod) userInfo:nil repeats:YES];
Quoting: NSTimer timerWithTimeInterval: not working
scheduledTimerWithTimeInterval:invocation:repeats: and scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: create timers that get automatically added to an NSRunLoop, meaning that you don't have to add them yourself. Having them added to an NSRunLoop is what causes them to fire.
There is two-option.
If using a timerWithTimeInterval
use a following like it.
refreshTimer = [NSTimer timerWithTimeInterval:1.0f target:self selector:#selector(timerHandler) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:refreshTimer forMode:NSRunLoopCommonModes];
also mode is two-option. NSDefaultRunLoopMode vs NSRunLoopCommonModes
more Information. refer a this documentation: RunLoopManagement
If using a scheduledTimerWithTimeInterval
use a following like it.
refreshTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:#selector(timerHandler) userInfo:nil repeats:YES];
scheduled timers are automatically added to the run loop.
more information. refer a this documentation: Timer Programming Topics
In summary
The "timerWithTimeInterval" you have to remember
to add the timer to the run loop that you want to add on.
The "scheduledTimerWithTimeInterval" default auto creates a timer that runs in
the current loop.

IOS: stop a NSTimer [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
NSTimer doesn't stop
I have this code:
[NSTimer scheduledTimerWithTimeInterval:110.0
target:self
selector:#selector(targetMethod:)
userInfo:nil
repeats:YES];
- (void) targetMethod:(NSTimer) timer{
NSLog(#"Hello World");}
in targetMethod I can stop timer with [timer invalidate], but out of this method, How can I stop targetMethod?
You can keep your NSTimer in a variable and stop the timer using the invalidate method. Like the following:
NSTimer * myTimer = [NSTimer scheduledTimerWithTimeInterval:110.0
target:self
selector:#selector(targetMethod:)
userInfo:nil
repeats:YES];
[myTimer invalidate];
One way to do it is to create NSTimer *timer; as a global variable so you can have a handle to the timer. Some thing likes this:
NSTimer *timer; //global var
timer = [NSTimer scheduledTimerWithTimeInterval:110.0
target:self
selector:#selector(targetMethod:)
userInfo:nil
repeats:YES];
To stop timer somewhere in the same class:
[timer invalidate];

Resources