dispatch_queue_t SerialQueue how check if is still running. ios GCD

谁都会走 提交于 2019-12-08 08:59:51

问题


I need to do a validation and for this I need to know if the task is still running i use this code for launch the task

    ` SerialQueue = dispatch_queue_create("miColaEnSerie", NULL);
       dispatch_async(SerialQueue, ^{
    [self loadImageFriend:init finalWhitNumber:final img1WithArray:infoImages1 img2WithArray:infoImages2 img3WithArray:infoImages3];
    });`

回答1:


If you have only one task at a time in your SerialQueue then you can add atomic property to a class like:

@property (assign) BOOL isSerialQueueRunning;

Then:

 isSerialQueueRunning = YES;
 dispatch_async(SerialQueue, ^{
    [self loadImageFriend:init finalWhitNumber:final img1WithArray:infoImages1 img2WithArray:infoImages2 img3WithArray:infoImages3];
    isSerialQueueRunning= NO;
 });

If however there are more tasks you add to SerialQueue you could change BOOL isSerialQueueRunning to NSInteger serialQueueTasks and increment/decrement it accordingly. Then verify if it's 0 which means nothing is in the queue at the moment.

Sometimes it's better to implement notifications to get information when the task is done and perform some action after that.



来源:https://stackoverflow.com/questions/13033165/dispatch-queue-t-serialqueue-how-check-if-is-still-running-ios-gcd

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!