如何等待函数调用以Swift结束

时间:2016-02-04 20:06:33

标签: swift asynchronous

我的问题很简单,但经过多次研究和测试后,在继续流程之前,我没有等到功能结束。

示例:

print("Before stuff")
do_stuff {
    print("After stuff")
}


func do_stuff(onCompleted: () -> ()) {
    let postEndpoint: String = "http://localhost:8080/users/1234567890987654"
    guard let url = NSURL(string: postEndpoint) else {
        print("Error: cannot create URL")
        return
    }
    let urlRequest = NSURLRequest(URL: url)
    let config = NSURLSessionConfiguration.defaultSessionConfiguration()
    let session = NSURLSession(configuration: config)
    let task = session.dataTaskWithRequest(urlRequest, completionHandler: {
    (data, response, error) in
    guard let responseData = data else {
        print("Error: did not receive data")
        return
    }
    guard error == nil else {
        print("error calling GET on /users/1234567890987654")
        print(error)
        return
    }
    // parse the result as JSON
    let user: NSDictionary
    do {
        user = try NSJSONSerialization.JSONObjectWithData(responseData,
            options: []) as! NSDictionary
    } catch  {
        print("error trying to convert data to JSON")
        // Means that user does not exist
        return
    }
    print("The user is: " + user.description)
    })
    task.resume()
    onCompleted()
}

如何在第二次打印之前等待do_stuff()结束?

感谢您的帮助,我想我错过了什么。

托马斯

2 个答案:

答案 0 :(得分:1)

你有一些基本的东西是你无法理解的。它实际上并不是正在执行的功能。它是函数内部定义的闭包。您需要等待的是在请求完成时调用的闭包。事实上,你不应该等待,但是在函数外部分配另一个闭包来从函数内部的闭包中调用。

print("Before stuff")
do_stuff {
    // Now the "function" has completed.
    print("After stuff")
}

func do_stuff(onCompleted: () -> ()) {
    let task = session.dataTaskWithRequest(urlRequest) { data, response, error in
        ...
        onCompleted()
    }
}

答案 1 :(得分:0)

您总是可以使用do_stuff()函数的完成处理程序:

func do_stuff(completion: (() -> Void)?) -> () {
...
    if completion != nil {
        return completion!() // Return completion where you want you "after stuff" declarations to run.
    }
}

并按原样调用该函数:

do_stuff({
    print("After stuff")
})