gpt4 book ai didi

ios - 如何顺序执行两个异步函数

转载 作者:可可西里 更新时间:2023-11-01 01:00:05 26 4
gpt4 key购买 nike

func getTopicIdFromMYSQL(){
let myUrl = NSURL(string: "xxxx")
let request = NSMutableURLRequest(URL: myUrl!)
request.HTTPMethod = "POST"
let email:String = "xxx@gmail.com"
let postString = "email=\(email)"
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request){
data, response, error in
if(error != nil){

print("Get all topic")
print("error=\(error)")
return
}
do {
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary

if let parseJSON = json
{
let resultValue = parseJSON["status"] as? String

print("Get all topic")
favouriteTopic = parseJSON["getResult"]! as! [AnyObject]
print("return topic:\(favouriteTopic)")

dispatch_async(dispatch_get_main_queue(), {

if(resultValue == "Success"){

}
else{
let error = UIAlertController(title: "Error", message: "Please check your network configuration!:-(", preferredStyle: .Alert)
let cancel = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
let ok = UIAlertAction(title: "OK", style: .Default, handler: nil)
error.addAction(cancel)
error.addAction(ok)
}
})

}

}catch
{
print(error)
}

}
task.resume()

}我已经有了这段代码,但是,我想在它之后运行另一个函数。我应该怎么办。喜欢:

getTopicIdFromMYSQL()
getCommentFromMYSQL()
print("Finish")

我发现问题是我的代码没有按顺序执行,getCommentFromMYSQL函数和getTopicIdFromMYSQL几乎一样,我想按顺序运行这三个怎么办?

最佳答案

将完成处理程序作为参数添加到您的异步函数:

func getTopicIdFromMYSQL(completion: (AnyObject?, ErrorType?)->())

func getCommentFromMYSQL(completion: (AnyObject?, ErrorType?)->())

注意:完成处理程序必须在异步函数完成时最终被调用——无论是错误还是计算值。

然后您可以调用这些函数,如下所示:

getTopicIdFromMYSQL() { (result1, error) in
if let result1 = result1 {
// process result1
// ...
getCommentFromMYSQL() { (result2, error) in
if let result2 = result2 {
// process result2
// ...
} else {
// handle error
}
}
} else {
// handle error
}
}

您可以按如下方式实现这些功能:

func getTopicIdFromMYSQL(completion: (AnyObject?, ErrorType?) {
let myUrl = NSURL(string: "xxxx")
let request = NSMutableURLRequest(URL: myUrl!)
request.HTTPMethod = "POST"
let email:String = "xxx@gmail.com"
let postString = "email=\(email)"
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request){
data, response, error in
if error != nil {
completion(nil, error)
}
do {
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary
if let parseJSON = json {
let resultValue = parseJSON["status"] as? String
print("Get all topic")
favouriteTopic = parseJSON["getResult"]! as! [AnyObject]
completion(favouriteTopic, nil)
} else {
throw MyError.Error(message: "bogus JSON")
}
} catch let error {
completion(nil, error)
}
}
task.resume()
}

关于ios - 如何顺序执行两个异步函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35551771/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com