📂
JackyChen的精神時光屋
  • About
  • iPlayground
    • iPlayground 2020
      • iPlayground submit 2020
    • iPlayground 2019
      • Untitled
      • iPlayground submit 2019
  • WWDC
    • 2020 WWDC
    • 2019 WWDC
    • 2018 WWDC
      • What's New in Testing
      • BusinessChat
    • 2016 WWDC
      • What's New in the Apple Push Notification Service
  • AR/VR
    • ARKit plugin at Unity
    • ARKit
    • AR/VR 實習作品分享
    • Google Blocks
  • CI/CD
    • Continous Integretion for Unity
    • 拯救地球大作戰-自動化設定注意事項
    • Provisioning Profile 自動化更新
    • Make ipa file with personal team of code sign
    • Xcode11 版號問題
  • Test
    • Cucumberish
    • XCUITest
    • Design Patterns in XCUITest
    • Unit Test
  • User Interface
    • IBDesignable 和 IBInspectable
    • iOS 使用貝塞爾曲線繪製路徑
    • UIStatusBarStyle
    • iOS Devices Specification
    • Vector Image
    • Launch Screen
    • Haptic Feedback
    • Good Works for Storyboard
    • Cell 展開收合效果
    • ScrollView
    • Swift lazy
    • Lottie
  • Foundation
    • Adding a Custom Font to Your App
    • WKWebView how to work with javascript
    • Global Central Dispatch
    • HealthKit
    • Error Handling
    • Debug with LLDB
    • Application Lifecycle
    • Swift Codable
    • Push Notifications
    • AVFoudation
  • Others
    • C語言指標概念
    • UnsafePointer(Swift)
    • iOS News Reference
    • Blender
    • Free Web Server
    • Firebase
    • Firebase migration
    • GraphQL
    • Ruby
    • zsh command line
    • visudo
  • Security
    • 課程:App資安規劃與實作
    • KeyChain
    • iOS反組譯程序
    • Arxan
  • Git
    • Git
    • xcodeproj 合併衝突
    • Pull Request
  • Machine Learning
    • CoreML
    • Vision
    • Turi Create
  • 待分類
    • ABI Stability and More
    • Mirror
    • Carthage
    • SwiftUI
    • MVVM
    • OpenSSL
    • USDZ Convert
    • Nexus repository and gitlfs
Powered by GitBook
On this page
  • 教學連結
  • 優先級:DispatchQoS
  • semaphore

Was this helpful?

  1. Foundation

Global Central Dispatch

PreviousWKWebView how to work with javascriptNextHealthKit

Last updated 5 years ago

Was this helpful?

教學連結

    • 使用 DispatchWorkItem 進行可取消任務的延遲工作

    • 使用 DispatchGroup 進行分組或者鏈接任務

    • 使用 DispatchSemaphore 等待異步任務

    • serial、concurrent 是針對在同一個 queue 內的 Task 產生效果

      • serial:Task 會依序執行

      • concurrent:Task 不會依序執行

    • sync、async 是針對 queue 產生的效果

      • sync:queue 依照前後順序執行

      • async:queue 不會依照前後順序執行

優先級:DispatchQoS

以前 GCD 的默認隊列優先級有四個:

DISPATCH_QUEUE_PRIORITY_HIGH

DISPATCH_QUEUE_PRIORITY_DEFAULT

DISPATCH_QUEUE_PRIORITY_LOW

DISPATCH_QUEUE_PRIORITY_BACKGROUND

現在改為了 QoSClass 列舉

 public enum QoSClass 
 {
    case background

    case utility

    case `default`

    case userInitiated

    case userInteractive

    case unspecified

    public init?(rawValue: qos_class_t)

    public var rawValue: qos_class_t { get }
}
  • 現在通常使用 QoS 為以下四種,從上到下優先級依次降低

    • User Interactive: 和用戶交互相關,比如動畫等等優先級最高。比如用戶連續拖拽的計算

    • User Initiated: 需要立刻的結果,比如push一個ViewController之前的數據計算

    • Utility: 可以執行很長時間,再通知用戶結果。比如下載一個文件,給用戶下載進度

    • Background: 用戶不可見,比如在後台存儲大量數據

semaphore

主要有三個 function

  • dispatch_semaphore_create : 建立 semaphore 並設定初始 count

  • dispatch_semaphore_wait : 等待信號,遇到時先將 count - 1。然後若 count < 0 則卡住線程給他等,直到 count >= 0 才放行

  • dispatch_semaphore_signal : 發送信號,將 count + 1

- (void)theMethod 
{
  // 1. 建立 semaphore,count = 0
  dispatch_semaphore_t mySemaphore = dispatch_semaphore_create(0);

  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    // (做耗時的 A)

    // 3. 遇到 signal,count = 0
    dispatch_semaphore_signal(mySemaphore);
  });

  // 2. 遇到 wait,count = -1,會一直停在這裡等待 count = 0
  // 4. 偵測到 count = 0,放行
  dispatch_semaphore_wait(mySemaphore, DISPATCH_TIME_FOREVER);

  // (做 B)
}
深入研究GCD
從使用場景了解GCD新API
iOS多線程Swift4 GCD深入解析
以 Semaphore 控制線程執行順序
Swift 3學習指南:重新認識GCD應用