calling method repeatedly after 3 seconds time interval in background - ios

I have gone through many sites but still no answer.
I have a method suppose void xyz(), which will get called automatically from a View Controller after every 3 seconds.
I have no idea what to use, do I have to use NSThread or PerformSelector.

Call this method from ViewDidLoad method.ViewDidLoad will when your view will be appear in iPhone device or Simulator.
[NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:#selector(runMethod) userInfo:nil repeats:YES];
-(void)runMethod
{
}

Something like this
-(void)xyz{
[self performSelectorInBackground:#selector(xyz) withObject:nil];
}
- (void)viewDidLoad {
[self performSelector:#selector(xyz) withObject:nil afterDelay:0.3];
}

Use NSTimer
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:#selector(xyz) userInfo:nil repeats:YES];

You should use NSTimer as mentioned by #mokujin.
Please visit https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/Reference/NSTimer.html

Related

How to set time interval api call in ios objective c and reload the table view after getting success

I am new to ios development .I have issue . I need to call api after set time interval (ios objective c) . After getting success it should load the table view .
Below is what I would do...
Step 1 : Call webservice [self makeWebserviceCall]
Step 2 : When webservice call is done, use NSTimer in - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(makeWebserviceCall) userInfo:nil repeats:NO];
^^^ --> change this to increase more
That's it...
You can use the following code :
// Delay execution of my block for 10 seconds.
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
// Call API
});
You can use NSTimer class for this
[NSTimer scheduledTimerWithTimeInterval:YOUR_TIME_INTERVAL target:self selector:#selector(apiCall:) userInfo:nil repeats:YES];
or use
[self performSelector:#selector(apiCall:) withObject:nil afterDelay:YOUR_TIME_INTERVAL];
and in apiCall method
[self performSelector:_cmd withObject:nil afterDelay:YOUR_TIME_INTERVAL];
But I think this way less clear
#define kAutoInterval 5.00f
[NSTimer scheduledTimerWithTimeInterval:kAutoInterval target:self selector:#selector(startUpdatingValues) userInfo:nil repeats:NO];

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

iOS Timer loop that executes a certain action every X minutes

I am trying to execute a certain block of code every x amount of time, but it seems that all I am doing is executing it during that time. Here's a block of my code.
while (TRUE) {
NSTimer *countDown = [NSTimer
scheduledTimerWithTimeInterval:(x)
target:self
selector:#selector(timerHandle)
userInfo:nil
repeats:YES];
}
Any ideas as to how to do it?
As written, this is an infinite loop, creating an NSTimer every loop iteration.
Try it without the while loop. This should cause [self timerHandle] to be invoked on interval x by a single background thread/timer. The Apple guide to NSTimer usage (including as others point out, how to properly stop your timed task) is here.
Try this: (It will call executeMethod on every 5 sec)
if (![NSThread isMainThread]) {
dispatch_async(dispatch_get_main_queue(), ^{
[NSTimer scheduledTimerWithTimeInterval:5.0
target:self
selector:#selector(executeMethod)
userInfo:nil
repeats:YES];
});
}
else{
[NSTimer scheduledTimerWithTimeInterval:5.0
target:self
selector:#selector(executeMethod)
userInfo:nil
repeats:YES];
}
Write the code you want to be executed in executeMethod method. Hope this helps.. :)

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

Resources