Frank's Book
  • Introduction
  • Privacy Policy
  • Git
    • [Git]Fully delete a git repository created with init
  • iOS
    • iOS Book
      • [iOS] Swift Coding Style
      • [iOS] UnitTest With XCode
      • [iOS] Build a Universal Framework for iOS using Swift
      • [iOS] Memory Management
      • [iOS] GCD And Operation Queue
      • [iOS] MVVM
      • [iOS] CoreData
      • [iOS] KVO & KVC
      • [iOS] Asynchronous Operation
      • [iOS] URLSession
      • [iOS] NSTimer & CADisplayLink
      • [iOS] UITextField & UITestView
      • [iOS] UITableView & UICollectionView
      • [iOS] Codable
      • [iOS] Array的sort, filter, map, reduce 函式
      • [iOS] Class vs Struct
      • [iOS] Reference Type vs Value Type
      • [iOS] Object
      • [iOS] IBDesignable & IBInspectable
  • Jenkins
    • Jenkins & Docker
      • [Docker] Docker Commands
      • [Docker] 1. Setup Jenkins with Docker on Mac
      • [Docker] 2. Jenkins建立Mac子節點
      • [Docker] 3. Mac節點build Xcode project設定
      • [Jenkins] 1. Install Jenkins
      • [Jenkins] 2. Change Default User Of Jenkins
      • [Jenkins] 3. Integration with Git
      • [Jenkins] 4. Integration with XCode
      • [Jenkins] 5. Integration with Unit Test
      • [Jenkins] 6. Uninstall Jenkins
      • [Jenkins] 7. Jenkins Commands
      • [Jenkins] 8. Jenkins Plugins
      • [Jenkins] 9. Problem Solving
  • Flutter
    • Flutter Book
      • [Flutter] Update your Flutter path (Mac OS)
      • [Flutter] Release Command
      • [Flutter] Life Cycles
      • [Flutter] AppLifecycleState
      • [Flutter] Navigator Pop時回傳資料
      • [Flutter] Install appium-flutter-driver
  • Leet Code
    • LeetCode Solutions
      • [LeetCode] 1. Two Sum [Easy]
      • [LeetCode] 2. Add Two Numbers [Medium]
      • [LeetCode] 3. Longest Substring Without Repeating Characters [Medium]
      • [LeetCode] 5. Longest Palindromic Substring [Medium]
      • [LeetCode] 7. Reverse Integer [Easy] [LeetCode]
      • [LeetCode] 8. String to Integer (atoi)
      • [LeetCode] 11. Container With Most Water
      • [LeetCode] 13. Roman to Integer
      • [LeetCode]14. Longest Common Prefix
      • [LeetCode] 15. 3Sum
      • [LeetCode] 17. Letter Combinations of a Phone Number
      • [LeetCode] 19. Remove Nth Node From End of List
      • [LeetCode] 20. Valid Parentheses
      • [LeetCode] 21. Merge Two Sorted Lists
      • [LeetCode] 22. Generate Parentheses
      • [LeetCode] 26. Remove Duplicates from Sorted Array
      • [LeetCode] 28. Implement strStr()
      • [LeetCode] 33. Search in Rotated Sorted Array
      • [LeetCode] 34. Find First and Last Position of Element in Sorted Array
      • [LeetCode]36. Valid Sudoku
      • [LeetCode] 38. Count and Say
      • [LeetCode] 46. Permutations
      • [LeetCode] 48. Rotate Image
      • [LeetCode] 49. Group Anagrams
  • Git
    • Git Commands
      • [LeetCode] 50. Pow(x, n)
  • About Author
    • Frank Chen
Powered by GitBook
On this page
  • Dispatch Queues
  • 延遲執行
  • Main Queues
  • Global Queues
  1. iOS
  2. iOS Book

[iOS] GCD And Operation Queue

Dispatch Queues

(1.) 建立一個DispatchQueue

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

(2.) Sync

在Sync block完成前, 主執行緒會先暫停

  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執行時, 主執行會繼續執行

 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) 優先級

優先級越高, 越會先被執行.

由高順位至低順位分別為:

  • userInteractive

  • userInitiated

  • default

  • utility

  • background

  • unspecified

ex:

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會同步執行任務, 不再依序排隊執行

ex: 非Concurrent

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

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

延遲執行

// to run something after 0.1 seconds

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

Main Queues

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

Global Queues

DispatchQueue.global().async {

}
Previous[iOS] Memory ManagementNext[iOS] MVVM

Last updated 6 years ago