Tim
Tim

Reputation: 101

How to use Dispatch Queue issues on iOS?

I want to know what is the main rule for using own queues/threads on iOS with GCD.

GCD provide us with:

DispatchQueue.global()

to execute code in background. It's also possible to create own queues with:

DispatchQueue(label: "my own queue")

What is here the better way or the suggestion? Use the global() queues from GCD or create my own?

One example: I want to do some network stuff with the Network.Framework.

Here's example 1:

    let connection = NWConnection(host: "1.2.3.4", port: 40000, using: .udp)
    let queue = DispatchQueue(label: "network queue")
    connection.start(queue: queue)

Here's example 2:

    let connection = NWConnection(host: "1.2.3.4", port: 40000, using: .udp)
    let queue = DispatchQueue.global()
    connection.start(queue: queue)

What do you think? example1 vs example2

Upvotes: 1

Views: 651

Answers (2)

Rob
Rob

Reputation: 438212

tl;dr

You probably want to create a custom, serial queue for this example. You’d probably like the nice descriptive name, which simplifies debugging. And you probably want serial behavior on this queue so that it’s easier to reason about the logic and avoid introducing additional thread-safety concerns.


Regarding your general question:

Use the global() queues from GCD or create my own?

A few considerations that might dictate your choice of a custom queue over a global queue:

  1. Do you need serial behavior or concurrent behavior? Global queues are concurrent, so you wouldn't use them if you need serial behavior. (Note, if you want concurrent queue, that doesn’t necessarily mean you have to use global queues. You can create you own custom concurrent queues, too. My point is merely that if you want a serial behavior, you cannot use global queue.)

  2. Might you want to ever suspend the queue for any reason? You can’t suspend global queues.

  3. Do you ever want to use barriers? You can’t use barriers on global queues.

  4. Would you like to name your queue so that you can diagnose the relevant subsystem within your project (e.g., when you're looking at stack traces or analyzing with Instruments). You can provide meaningful names to custom queues that simplify debugging.

The above notwithstanding, I use global queues for simple, short, one-off sorts of tasks. I tend to use a well-named, custom queues for longer running tasks or for tasks associated to a particular logical subsystem within my app, for a better debugging experience.

Upvotes: 0

Shehata Gamal
Shehata Gamal

Reputation: 100543

This DispatchQueue.global() is a concurrent queue meaning Tasks submitted to the returned queue are scheduled concurrently with respect to one another.

While this DispatchQueue(label: "network queue") is a serial queue tasks submitted run one after the other

When yo use depends on your case , the option to create a queue is commonly used in frameworks/libraries for separating their work from your code , having a ready global queue is for handy use if you find that you overwhelm it by your usage then create a new one


Your example could be explained like , if you have that in your method that called each time when your connection changes

 print("1")
 print("2")
 print("3")

With global you'll get

 print("1")
 print("3")
 print("2") 
 print("3")
 print("1")
 print("2")

While with queue created

 print("1")
 print("2")
 print("3")

 print("1")
 print("2")
 print("3")

Upvotes: 1

Related Questions