Search code examples
iosobjective-cxcodeviewdidloadperformselector

perform selector after delay only called once


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


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.