Help us understand the problem. What is going on with this article?

【Swift 3】処理の完了を待ってから後続処理を行う

More than 3 years have passed since last update.

背景

非同期で幾つか処理をしている状態で、値の取得が完了したら後続処理を行いたい場合がありました。
セマフォで後続処理を行わないようにしていましたが、記述箇所が増えてしまい、
見通しが悪くなってきたので、待機条件と後続処理をBlockで記述できるようにExtensionを書きました。

コード

/// 条件をクリアするまで待ちます
///
/// - Parameters:
///   - waitContinuation: 待機条件
///   - compleation: 通過後の処理
func wait(_ waitContinuation: @escaping (()->Bool), compleation: @escaping (()->Void)) {
    var wait = waitContinuation()
    // 0.01秒周期で待機条件をクリアするまで待ちます。
    let semaphore = DispatchSemaphore(value: 0)
    DispatchQueue.global().async {
        while wait {
            DispatchQueue.main.async {
                wait = waitContinuation()
                semaphore.signal()
            }
            semaphore.wait()
            Thread.sleep(forTimeInterval: 0.01)
        }
        // 待機条件をクリアしたので通過後の処理を行います。
        DispatchQueue.main.async {
            compleation()
        }
    }
}

使い方

var imgData: NSData! = nil
DispatchQueue.global().async {
    imgData = NSData(contentsOf: URL(string: "http://hogehoge.com/fugafuga.png")!)
}
// dataを取得するまで待ちます
wait( { return imgData == nil } ) {
    // 取得しました
    print("finish")
    UIImage(data: imgData as Data)
}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした