Question

I have an app in which i need to call an instance method after every 1 or 2 seconds. Now if i place

[self performSelector:@selector(getMatchListWS) withObject:nil afterDelay:1.0];

in viewDidLoad: or viewWillAppear: , the method getMatchListWS is called only once as the view appears or loads. But i need to call the method continuously even when the user is on that view without the view being disappeared or unload. So, what is the correct place or delegate method in which i can add performSelector method so that it is called every second without having to unload the view again and again. Do i need to do something in background or on main thread.? Thanks in advance!!

Was it helpful?

Solution

It would be like this:

[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(getMatchListWS:) userInfo:nil repeats:YES];

Put it in your viewDidLoad, so you don't have problems with multiple events being fired. It can happen if you put it on viewWillAppear or viewDidAppear, and you are pushing or showing a modalViewController.

OTHER TIPS

Jacky Boy's answer will get your job done. An alternate solution(if you're keen on using performSelector method) would be to add the same line in your method definition like so

-(void) getMatchListWS {
//Get Match List here

[self performSelector:@selector(getMatchListWS) withObject:nil afterDelay:1.0];
}

Note: You should still call the method once when the view loads.

You are just keeping a delay in your call. What that will do, is to call your method after a delay of 1 second. What you need to do is to set the timer to call your method repeatedly after a particular time interval.

//create an instance of NSTimer class
NSTimer *timer;

//set the timer to perform selector (getMatchListWS:) repeatedly
timer= [NSTimer timerWithTimeInterval:1.0
                                        target:self
                                        selector:@selector(getMatchListWS:)
                                        userInfo:nil
                                        repeats:YES];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top