Skip to content

Instantly share code, notes, and snippets.

@tailang
Last active October 12, 2017 10:50
Show Gist options
  • Save tailang/a399ed1cb31c9204d5ec9d236bb59b6a to your computer and use it in GitHub Desktop.
Save tailang/a399ed1cb31c9204d5ec9d236bb59b6a to your computer and use it in GitHub Desktop.
在子线程跑一个timer
dispatch_async(dispatch_get_global_queue(0, 0), ^{
        _link = [CADisplayLink displayLinkWithTarget:self selector:@selector(tick:)];
        // NOTE: 子线程的runloop默认不创建; 在子线程获取 currentRunLoop 对象的时候,就会自动创建RunLoop
        // 这里不加到 main loop,必须创建一个 runloop
        NSRunLoop *runloop = [NSRunLoop currentRunLoop];
        [_link addToRunLoop:runloop forMode:NSRunLoopCommonModes];
        // 必须 timer addToRunLoop 后,再run
        [runloop run];
    });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment