call method every 5 minutes in my UIView - iphone

I want to call a certain method in my UIView code say every 5 minutes - how do I accomplish that?

You can use an NSTimer:
Put the following in your viewDidLoad (where 300 is the number of seconds):
[NSTimer scheduledTimerWithTimeInterval:300.0f
target:self
selector:#selector(updateMethod:)
userInfo:nil
repeats:YES];
And then create your update method:
- (void)updateMethod:(NSTimer *)theTimer {
// Your code goes here
}

You can use NSTimer to do this.
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/Reference/NSTimer.html
Specifically, the
timerWithTimeInterval:target:selector:userInfo:repeats:
class method.

There is an easy way to do this, in you update method
-(void)update{
//Your update methods here
[self performSelector:#selector(update) withObject:nil afterDelay:300.0];
}

Related

call function after defined seconds without using timer

I am calling a function after 180 seconds using timer. But the timer sometimes behaves in different manner and calls the function multiple times in 180 seconds.
please suggest me how to call a function after each 180 seconds untill I post stop message without using timer.
Code
if(!tmr_CallWebService)
{
tmr_CallWebService = [NSTimer scheduledTimerWithTimeInterval:180 target:ClassTracing selector:#selector(startLocationTracing) userInfo:nil repeats:YES];
}
Thanks in advance
I think there is a method to do this task, here use it like this
[self performSelector:#selector(yourMethodName) withObject:[NSArray arrayWithObjects:firstArgument,secondeArgument,nil] afterDelay:180];
use nil in place of array if you have no argument in that method.
You can use - (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay from NSObject

Write code instead of selector in like NSTimers?

Is there anyway to write code instead of setting a selector to method to call in NSTimer?
If i want to print hello world after 5 sec i can do it like this.
[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:#selector(helloWorld:) userInfo:nil repeats:NO];
and have this function
-(void)helloWorld:(NSTimer*)aTimer {
NSLog(#"Hello World!");
}
But instead of writeing functions for every timer you have is it possible to add NSLog(#"Hello World!") in the same line as where i create the timer?
I use this code– an NSTimer category:
https://gist.github.com/250662/d4f99aa9bde841107622c5a239e0fc6fa37cb179
Some methods allow you to pass a code block as a parameter. Unfortunately this isn't supported for NSTimer.

I want to call a method after particular time until my app opened in iphone?

I want to call a method after each 2 minutes,
how can I apply such logic?
Use an NSTimer object. It's been talked about quite a lot here, so I won't repeat all that - just search for "NSTimer" in the search box at the top right. :-)
You can use a timer..
[NSTimer scheduledTimerWithTimeInterval:120.0 target:self selector:#selector(yourFunction:) userInfo:nil repeats:YES];
.........
..........
-(void)yourFunction:(NSTimer*)timer{
//do your action here
}
I use a NSTimer to trigger a method. I use the below code in my initial viewDidLoad section:
myTimer = [NSTimer scheduledTimerWithTimeInterval:120.0 target:self selector:#selector(theMethod) userInfo:nil repeats:YES];
Hope that helps.

I should able to delay 2 seconds using NSTimer. How to do it?

I want to produce a delay of 2 seconds using NSTimer how to initialize timer in program?
Multiple options here.
If you just want a delay of 2 seconds you could use the sleep() function
#include<unistd.h>
...
sleep(2);
Or you may be able to use NSTimer like so
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(fireMethod) userInfo:nil repeats:NO];
And in your class you would have a method defined as
-(void)fireMethod
{
//Do stuff
}
Here you go...
[NSTimer scheduledTimerWithTimeInterval:2
target:self
selector:#selector(action)
userInfo:nil
repeats:NO];
Simple answer: [NSThread sleepForTimeInterval:10.0];
Note that you should not really be thinking about delays in an event driven UI/OS. You should thinking about tasks you want to do now, and tasks you want to do later, and code these subtasks and schedule them appropriately. e.g. instead of:
// code that will block the UI when done in the main thread
- (void) methodC {
doA();
delay(2);
doB();
}
you might want to have code that looks more like:
- (void) methodA {
doA();
return; // back to the run loop where other useful stuff might happen
}
- (void) methodB {
doB();
}
and you can then schedule methodB with an NSTimer at the end of methodA, an NSTimer started by whatever called methodA, or, the best option, by the asynchronous completion routine of something started by methodA.

NStimer -- what am I doing wrong here?

I've been using an NSTimer successfully, but am now having trouble with it. Undoubtably something stupid. Appreciate another set of eyes. Running the debugger, I see that applicationDidFinishLaunching is called, but trigger is never called.
-(void) trigger:(NSTimer *) theTimer{
NSLog(#"timer fired");
}
- (void)applicationDidFinishLaunching:(UIApplication *)application {
nst = [NSTimer timerWithTimeInterval:1.0 target:self selector:#selector(trigger) userInfo:nil repeats:YES];
[window makeKeyAndVisible];
}
The selector must have the following signature:
- (void)timerFireMethod:(NSTimer*)theTimer
so you need
#selector(trigger:)
--edit--
Maybe you are doing this somewhere else, but in the code you included you do not actually start the timer. You have to add it to a NSRunLoop before it can trigger any events at all.
[[NSRunLoop currentRunLoop] addTimer:nst forMode:NSDefaultRunLoopMode];
If I read the examples correctly. I've only used the one the init method that automatically adds it to the current NSRunLoop. You really should look at the developer docs that someone included in the comments to my post.
Two things:
1) as others say, the method should have the following signature..
-(void) trigger:(NSTimer *) theTimer;
and you make the timer thus:
nst = [NSTimer timerWithTimeInterval:1.0 target:self selector:#selector(trigger:) userInfo:nil repeats:YES];
2) merely creating the timer does not run it. As the documentation says:
You must add the new timer to a run
loop, using addTimer:forMode:. Then,
after seconds have elapsed, the timer
fires, invoking invocation. (If the
timer is configured to repeat, there
is no need to subsequently re-add the
timer to the run loop.)
Here's a piece of real functioning code that you can model after. The timer creation is the same as yours, but it also adds it to runloop the right way.
[[NSRunLoop currentRunLoop] addTimer:
[NSTimer timerWithTimeInterval:0.1
target:self
selector:#selector(someSelector:)
userInfo:nil
repeats:NO]
forMode:NSDefaultRunLoopMode];
The selector you're giving the timer, trigger, indicates that it should call a method that takes no parameter. Either change your timer-fired method to
- (void)trigger
{
// look at me, I don't take any parameters
NSLog(#"timer fired");
}
or change your initial timer call to use #selector(trigger:).
Your problem is due to the fact that timerWithTimeInterval:target:selector:userInfo:repeats: creates a timer but does not schedule it on the run loop, you have to do it yourself.
However, you may as well use this method which creates the timer and schedules it on the run loop: scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
I had a problem when starting timer in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { not in main thread.
dispatch_async(dispatch_get_main_queue(), ^{
[self startScheduledTimer];
});