在segue之前等待异步方法

时间:2016-02-10 11:44:41

标签: multithreading swift asynchronous request

我有一个按钮,当我按下它时我正在调用异步功能:

func check(url : String){
        let url = NSURL(string: url)
        print("Checking")
        dispatch_async(dispatch_get_main_queue(), {
            let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
                if error == nil {
                    self.isWorking = true
                }
                else{
                    self.isWorking = false
                    }
                }
            }
            task.resume()
        })
    }

所以当我按下按钮时,我会执行以下操作:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!){
        let web = segue.destinationViewController as! ViewController()

        check(url)

        dispatch_async(dispatch_get_main_queue(), {
              if (isWorking){ 
                   // Do stuff
              }
      })
}

问题是在检查方法完成之前调用isWorking。

在我检查isWorking之前,如何确保检查完成?

3 个答案:

答案 0 :(得分:2)

您可以使用“完成处理程序”:

func check(url : String, completion: (isWorking: Bool)->()) {
    let url = NSURL(string: url)
    print("Checking")
    let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
        if error == nil {
            completion(isWorking: true)
        }
        else{
            completion(isWorking: false)
        }
    }
    task.resume()
}

你这样称呼它,带有一个尾随的闭包:

check(url) { (isWorking) in
    if isWorking {
        // do stuff
    } else {
        // not working
    }
}

答案 1 :(得分:1)

这将完成这项工作:

import UIKit

class AnyViewController: UIViewController
{
    var isWorking = false

    func check(url : String)
    {
        let url = NSURL(string: url)
        print("Checking")

        let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
            if error == nil
            {
             self.performSegueWithIdentifier("mySegueID", sender: nil)
            }
            else{
                self.isWorking = false
            }
        }

        task.resume()
    }
}

答案 2 :(得分:0)

只有当任务完成后,您才可以向侦听器发布通知以执行segue NSNotificationCenter.defaultCenter()。postNotificationName(“name”,object:nil):

override func viewDidLoad() {
    super.viewDidLoad()

    // Register a notification listener
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "customSegue:", name:"segue", object: nil)
}

func customSegue(notification: NSNotification){
    // Do stuff
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!){
    check(url)
}

func check(url : String){
    let url = NSURL(string: url)
    print("Checking")
    dispatch_async(dispatch_get_main_queue(), {
        let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
            if error == nil {
                self.isWorking = true
                // Send a notification to the listener to perform stuff
                NSNotificationCenter.defaultCenter().postNotificationName("segue", object: nil)
            }
            else{
                self.isWorking = false
                }
            }
        }
        task.resume()
    })
}

这样,customSegue func中的代码只会在error == nil

时执行