0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

waitUntilCompleted iOSの非同期処理を同期にする

Posted at

概要

最近仕事で非同期を同期にして処理するニーズがあったので、古いコードでライブラリも使えないので、GCD使って簡単に実装してみました。

let task = Task()

task.doSomeThingAsync()
task.waitUntilCompleted()

print("finished")

詳細

  • DispatchSemaphore インスタンス生成
  • semaphoreインスタンスqueue止めたいところwait呼ぶ
  • 非同期処理終わったらsemaphoreインスタンス signal呼ぶ

コード

class Task {
    var semaphore: DispatchSemaphore?
    
    func doSomeThingAsync() {
        semaphore = DispatchSemaphore(value: 0)
        DispatchQueue.global().async { [weak self] in
            print("doing task...")
            sleep(1)
            print("task done!")
            
            // 実行終了
            self?.semaphore?.signal()
            self?.semaphore = nil
        }
    }
    
    func waitUntilCompleted(_ timeout: TimeInterval = 0) {
        // 0の場合はtimeout設定しない
        if timeout == 0 {
            semaphore?.wait()
            return
        }
        semaphore?.wait(timeout: .now() + .milliseconds(Int(timeout * 1000)))
    }
}


注意:

waitだとsignal呼ばないとqueueをブロックしてしまうので、実行queueはmain queueではなく別のqueueにするか、func wait(timeout: DispatchTime) 使ったほうが無難.

 参照

DispatchSemaphore

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?