问题
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