1

I can't find any reference on doing something that should be really basic: I'd like to have a method called "forever" on the main UI loop. I would be happy both with an way to call my method synced with the UI refresh rate, or by passing a custom time granularity, as I don't really need it to happen more often than every 50-100 ms. Both answers for C++ (Carbon) and Objective C are fine, even though I will eventually use it in a pure C++ application. If you could suggest also how to remove this timer, it would be great.

Please check the comments for a further explanation of the threading scenario where I want to use this.

something like

class MySyncedClass {
void start() {
    // start calling "execute" member function on main loop every N ms
}

void stop() {
   // stop calling "execute"
}

void execute() {
    // do something
}

};
Alex Darsonik
  • 597
  • 5
  • 18
  • why would you do that? – peko May 27 '13 at 16:13
  • each time execute() is called, it will check the status of another thread and fire a user-provided callback that I want to make sure on being on the main thread to make the user code easier to write and single-threaded. Also, you can imagine having other "doSomething" methods that will be triggered by the UI user code, and I want to make sure that both these methods and the internal computations scheduled by execute() happen on the same thread. makes sense? – Alex Darsonik May 27 '13 at 16:18

2 Answers2

2

usually you do something like this in the application delegate in the method didFinishLaunchingWithOptions. When you use an ivar to save the current instance of NSTimer, you can stop it anytime.

@implementation AppDelegate {
    NSTimer *_timer;
}

- (void) start {
    _timer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(execute) userInfo:nil repeats:YES];
}

- (void) stop {
    //reset timer
    if (_timer != nil) {
        [_timer invalidate];
        _timer = nil;
    }
}

- (void) execute {
    //do something
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
     [self start];
  }

but you should use a callback via block if you only want to check another threads status.

if you want to execute something on the main thread from another thread you can use dispatch_get_main_queue:

dispatch_async(dispatch_get_main_queue(), ^{
             //do something
         });
Herm
  • 2,956
  • 19
  • 32
  • what do you mean with "via block"? sorry, not a cocoa expert at all. The library is multi-platform so I'll wrap this platform dependent stuff into a member variable of MySyncedClass, that is MySyncedClass will be std C++ that just knows it's execute method will be called every N ms. – Alex Darsonik May 27 '13 at 16:27
  • 1
    in objective-c you can use closures (blocks) to execute code fragments as some kind of anonymous functions. For more information you should look at the documentation at http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/Blocks/Articles/bxGettingStarted.html#//apple_ref/doc/uid/TP40007502-CH7-SW1, you can use this blocks to perform callbacks or let the dispatcher execute something on another thread, as you can see in the second snippet above. – Herm May 27 '13 at 16:46
1
  1. Swift 3.0

    DispatchQueue.main.async(execute: { 
                        self._eventListTableView.reloadData()
                    })
    
abhi
  • 563
  • 6
  • 9