Search code examples
objective-cwatchkitwatchos

Watch OS2 - performSelector: does not work in controller


I'm trying to call a method + delay inside a Watch OS2 controller (WKInterfaceController) with delay using:

[self performSelector:@selector(createPages) withObject:nil afterDelay:3.0];

And the method:

- (void) createPages {
    NSLog(@"Creating pages");
}

The createPages method is never called. Not seeing any errors in the console. Seems like it's completely ignoring it. Does anyone know why this might be happening?

Thanks!

G


Solution

  • Using Grand Central Dispatch is a better approach for this kind of thing, and it also will work with statically dispatched methods if you're using Swift. You can execute some code after 3 seconds like this:

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self createPages];
    });
    

    Check out the Concurrency Programming Guide for more information.