开发者

How to determine if dispatch_queue_t is suspended in iOS?

开发者 https://www.devze.com 2023-01-15 09:43 出处:网络
Is there some way to know that a queue is suspended and i can resume it and vice-versa? Because otherwise app crashes. Or do i need to store this in开发者_开发百科formation in some var and check?BOOL

Is there some way to know that a queue is suspended and i can resume it and vice-versa? Because otherwise app crashes. Or do i need to store this in开发者_开发百科formation in some var and check?


BOOL dispatch_queue_is_empty(dispatch_queue_t queue)
{
    dispatch_group_t group = dispatch_group_create();

    dispatch_group_enter(group);
    dispatch_async(queue, ^{
        dispatch_group_leave(group);
    });

    int64_t maxWaitTime = 0.00000005 * NSEC_PER_SEC;
    BOOL isReady = dispatch_group_wait(group, maxWaitTime) == 0;

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
        dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
        dispatch_release(group);
    });

    return isReady;
}

To test

dispatch_queue_t queue = dispatch_queue_create("test", 0);

NSLog(@"Is empty %@", dispatch_queue_is_empty(queue) ? @"YES" : @"NO");

dispatch_async(queue, ^{
    for(int i = 0; i < 100; i++)
    {
        NSLog(@"... %i", i);
    }
});

NSLog(@"Is empty %@", dispatch_queue_is_empty(queue) ? @"YES" : @"NO");
0

精彩评论

暂无评论...
验证码 换一张
取 消