perform selector after delay only called once

user2268539 picture user2268539 · Apr 22, 2013 · Viewed 25.3k times · Source

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

Answer

Rui Peres picture Rui Peres · Apr 22, 2013

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.