# \[iOS] GCD And Operation Queue

## Dispatch Queues

(1.) 建立一個DispatchQueue

```swift
 let queue = DispatchQueue(label: "com.frost.queue")
```

(2.) Sync

&#x20;     在Sync block完成前, 主執行緒會先暫停

```swift
  func syncQueue() {
    let queue = DispatchQueue(label: "com.frost.queue")
    
    queue.sync {
      for i in 0...5 {
        print("\(i)")
      }
    }
    
    for i in 100...105 {
      print("\(i)")
    }
  }
  
  -------------------------------
  Output:
  1
  2
  3
  4
  5
  101
  102
  103
  104
  105
  
```

(3.) Async

在Sync block執行時, 主執行會繼續執行

```swift
 func syncQueue() {
    let queue = DispatchQueue(label: "com.frost.queue")
    
    queue.async {
      for i in 0...5 {
        print("\(i)")
      }
    }
    
    for i in 100...105 {
      print("\(i)")
    }
  }
  
  -------------------------------
  Output:
  1
  2
  101
  102
  3
  4
  103
  104
  5
  105
```

(3.) Quality Of Service (QoS)  優先級

&#x20;  優先級越高, 越會先被執行.

&#x20;   由高順位至低順位分別為:

* userInteractive
* userInitiated
* default
* utility
* background
* unspecified

&#x20;ex:

```swift
let queue1 = DispatchQueue(label: "com.frost.queue1", qos: DispatchQoS.userInitiated)
let queue2 = DispatchQueue(label: "com.frost.queue2", qos: DispatchQoS.unspecified)

queue1.async {
    for i in 0...5 {
     print("\(i)")
    }
}

queue2.async {
    for i in 100...105 {
     print("\(i)")
    }
}

 -------------------------------
  Output:
  1
  2
  101
  3
  4
  5
  102
  103
  104
  105
```

(4.) Concurrent Queues

Concurrent Queues會同步執行任務, 不再依序排隊執行&#x20;

ex: 非Concurrent

```swift
let queue = DispatchQueue(label: "com.frost.queue1", qos: .default)

queue.async {
    for i in 0...5 {
     print("\(i)")
    }
}

queue.async {
    for i in 100...105 {
     print("\(i)")
    }
}

 -------------------------------
  Output:
  1
  2
  3
  4
  5
  101
  102
  103
  104
  105
```

ex: Concurrent

```swift
let queue = DispatchQueue(label: "com.frost.queue1", qos: .default, attributes: .concurrent)

queue.async {
    for i in 0...5 {
     print("\(i)")
    }
}

queue.async {
    for i in 100...105 {
     print("\(i)")
    }
}

 -------------------------------
  Output:
  1
  101
  2
  102
  3
  103
  4
  104
  5
  105
```

## 延遲執行

```swift
// to run something after 0.1 seconds

DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
    // your code here
}
```

###

## Main Queues

```swift
DispatchQueue.main.async {
        // 更新UI操作
}
```

## Global Queues

```swift
DispatchQueue.global().async {

}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://frost.gitbook.io/iosbook/ios/ios-book/ios-gcd-and-operation-queue.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
