Search code examples
cocoanstimernstableview

Cocoa - Action every second


How can I set action to update some information every 1 second?

I am creating chat (using social network API) and I have to check for new messages.


Solution

  • Declare an instance of NSTimer in your .h file like this

    NSTimer *timer;

    then schedule it:

    timer = [NSTimer scheduledTimerWithTimeInterval:1 
                                             target:self   
                                           selector:@selector(checkForNewMessages) 
                                           userInfo:nil 
                                            repeats:YES];
    

    When you don't need the timer anymore do this:

    [timer invalidate];
     timer = nil;