DISPATCH_SOURCE_TYPE_TIMER那些容易坑的点

timer使用代码类似如下:

    dispatch_source_t timer;
    timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0));
    dispatch_source_set_timer(timer,
                              dispatch_time(DISPATCH_TIME_NOW, 5*NSEC_PER_SEC),
                              5*NSEC_PER_SEC,
                              0);
    dispatch_source_set_event_handler(timer, ^{
        dispatch_suspend(timer);
    });
    dispatch_resume(timer);

注意:

1 这个timer是使用引用计数的,千万别多线程共用一个timer,容易操作出问题,随机crash;

2 这个timer是使用引用计数的, dispatch_resume不能调用多了,调2次会crash;

3 这个timer是使用引用计数的,dispatch_suspend不能调多了,调多了,计数会有问题;

4 前后台相关;



猜你喜欢

转载自blog.csdn.net/demondev/article/details/78573631