0

I have more than 5 tasks working asynchronously using Grand central dispatch. How do I know which task will finish first and which will finish last ? I mean to say how do I know the order in which the task will finish? And if I want to stop any task or all the tasks simultaneously, how can I archive this ?

Tapan Raut
  • 73
  • 7
  • Update your question with relevant code (as text, no pictures). Clearly explain what issues you are having with the code. – rmaddy Jul 31 '18 at 01:45

1 Answers1

0

How do I know which task will finish first and which will finish last?

You really can't. Async tasks complete when they complete, there is no order presumed, or guaranteed.

I mean to say how do I know the order in which the task will finish?

If one task is a child of another task, the child will finish first, as the parent will likely depend on the child's result. Otherwise, in the general sense, there really isn't a defined way to determine which task will finish first. GCD will allocate the schedule, and resources as it sees fit.

If you know what resources are available, a smart programmer can try to set up the queue such that as one task finishes, the resources are made available to another similar task, so there is no waiting, but that is FAR beyond the scope of this question.

And if I want to stop any task or all the tasks simultaneously, how can I archive this ?

I presume you meant achieve, not archive.

Per How to stop/cancel/suspend/resume tasks on GCD queue, you can't stop a task, or group of tasks in flight using GCD. If you use NSOperationsQueue, you can do horrible things, but technically it isn't part of GCD.

GokuMizuno
  • 493
  • 2
  • 5
  • 14