dispatch_queue_t SerialQueue如何检查是否仍在运行. ios GCD [英] dispatch_queue_t SerialQueue how check if is still running. ios GCD
问题描述
我需要进行验证,为此,我需要知道任务是否仍在运行,我使用此代码来启动任务
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];
});`
推荐答案
如果您一次在SerialQueue中仅执行一项任务,则可以向以下类添加原子属性:
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;
然后:
isSerialQueueRunning = YES;
dispatch_async(SerialQueue, ^{
[self loadImageFriend:init finalWhitNumber:final img1WithArray:infoImages1 img2WithArray:infoImages2 img3WithArray:infoImages3];
isSerialQueueRunning= NO;
});
但是,如果有更多任务添加到SerialQueue中,则可以将BOOL isSerialQueueRunning
更改为NSInteger serialQueueTasks
并相应地增加/减少它.然后验证它是否为0,这表示队列中目前没有任何内容.
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.
这篇关于dispatch_queue_t SerialQueue如何检查是否仍在运行. ios GCD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!