Periodically call to a web service in ios - ios

I have a NSTimer like this:
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(sliderUpdate:) userInfo:nil repeats:YES];
-(void)sliderUpdate:(id)sender
{
int currentTime = (int)((newPlayer.currentTime.value)/newPlayer.currentTime.timescale);
slider.value=currentTime;
NSLog(#"%i",currentTime);
song.currentTime=currentTime;
int currentPoint=(int)((newPlayer.currentTime.value)/newPlayer.currentTime.timescale);
int pointMins=(int)(currentPoint/60);
int pointSec=(int)(currentPoint%60);
NSString *strMinlabel=[NSString stringWithFormat:#"%02d:%02d",pointMins,pointSec];
lblSlidermin.text=strMinlabel;
song.strslidermin=strMinlabel;
}
What I want to do, is, in each and every 45 seconds, call a web service:
Example: 1st call in 45 seconds, second in 90 seconds, etc.
How can I do this in this NSTimer?

You just have to modify your NSTimer for 45 seconds:
[NSTimer scheduledTimerWithTimeInterval:45 target:self selector:#selector(sliderUpdate:) userInfo:nil repeats:YES];

Related

have a reference to an object inside NSTimer selector

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

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
}
}

while loop using NSTimer

I want to create a NSTimer that runs for lets say 10 minutes. I then want to write a while loop aftewards delaying 10 minutes of time before the line afterwards is executed. For example.
NSTimer * countDown = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:#selector userInfo:nil repeats:NO];
while (countDown == stil running (hasnt reached 10 minute mark) ){
// kill 10 minutes of time
}//when 10 minutes is up
execute next line of code
First
The timeInterval parameter of scheduledTimerWithTimeInterval: method is in seconds. If you want in minutes, don't forget to multiply by 60.
Second
Why would you want to wait the countDown with a while like that? Just call the lines you want to execute 10 minutes later in the selector that NSTimer fires.
NSTimer * countDown = [NSTimer
scheduledTimerWithTimeInterval:(10.0 * 60)
target:self
selector:#selector(tenMinutesLater)
userInfo:nil
repeats:NO];
And then
-(void)tenMinutesLater
{
//put some code here. This will be executed 10 minutes later the NSTimer was initialized
}
Instead of a while loop, just make a new timer inside of a method called by your first timer.
NSTimer *countDown = [NSTimer scheduledTimerWithTimeInterval:600 target:self selector:#selector(startSecondTimer) userInfo:nil repeats:NO];
- (void)startSecondTimer
{
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:600 target:self selector:#selector(doSomething) userInfo:nil repeats:NO];
}
PS, the time interval is in seconds - 10 mins = 600 seconds, not 10.

iphone development: nstimer calling a function

I have a void function which just have NSLog(#"Call me"); in its body.
I call it in my view in every ten seconds by using
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:#selector(yourMethod) userInfo:nil repeats:YES];
But I want it to stop it after 5 iterations. However it goes to infinity. How can I do that?
1) Keep a global variable, that increments from 0 to 5.
int i = 0;
2) Incement this variable inside your timer function..
-(void) yourFunction:(NSTimer*)timer{
//do your action
i++;
if(i == 5){
[timer invalidate];
}
}
3) When creating timer
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:10
target:self
selector:#selector(yourMethod:) // <== see the ':', indicates your function takes an argument
userInfo:nil
repeats:YES];
You should take one counter, increment it every time when your method get called, count for 5 and then invalidate your timer using below code.
[timer invalidate];
To destroy the timer from the current loop, you should call [timer invalidate];
To determine five occurrences, you need to maintain a variable and increment its count each time. If it is equal to 5, call invalidate method.
First of all you need to declare an int and declare your NSTimer *timer, so we can stop it:
#interface AppDelegate : UIViewController {
int myInt;
NSTimer *timer;
}
To start the NSTimer you'll need to change just a bit of the code:
timer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:#selector(yourMethod) userInfo:nil repeats:YES];
Inside your void function you can make the verification to check if the code's running after 5 iterations:
- (void)myVoid{
NSLog(#"Call Me");
if (myInt == 5) {
[timer invalidate];
timer = nil;
}
myInt++;
}

Resources