2

I know creation of queues and able to execute single task but how can i execute multiple tasks in parallel.

Concurrent Queue ---->

let concurrentQueue = DispatchQueue(label: "com.some.concurrentQueue", attributes: .concurrent)
concurrentQueue.async {
    //executable code

}

BackgroundQueue without priorities default--->

DispatchQueue.global().async {
    //executable code
}

Backgroundqueue with priorities---->

DispatchQueue.global(qos: .userInitiated).async { //.userInteractive .background .default .unspecified
    //executable code
}

Getting back to the main Queue ---->

DispatchQueue.main.async {
     //executable code
}

All are asynchronous but how can i execute multiple methods at a time how should i code in swift.

1
  • Please don't try to remove your question by editing the question text. If you have a legitimate reason to want the question removed, you should ask a moderator or site admin for help, see meta.stackoverflow.com/questions/305484/… for more information. Commented May 11, 2018 at 7:06

1 Answer 1

6

If you have a for loop method that calls a method and you want to calls this methods concurrent, so just use this :

DispatchQueue.concurrentPerform(iterations: Int, execute: { (count) in
   doSomethingFor(count: count)
}

But if you have some individuals methods that you want to call concurrent, just do like this:

let concurrentQueue = DispatchQueue(label: "com.some.concurrentQueue", attributes: .concurrent)

concurrentQueue.async {
    //executable code
    myFirstMethod()
}

concurrentQueue.async {
    //executable code
       mySecondMethod()
}

This way concurrentQueue, will manages your tasks concurrently itself.

2
  • Its hard to handle this by DispatchQeue. Instead you could use Operation and OperationQeueu. In that situation you can set maximum Concurrent tasks that is what you want. Commented Apr 29, 2018 at 16:02
  • It's beyond of here to explain it, but if you want a good tutorial about that just have look at this tutorial: appcoda.com/ios-concurrency Commented Apr 30, 2018 at 7:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.