让我们提出这个场景
具有异步网络操作的方法
func asyncMethodA() -> String?
{
result : String?
Alamofire.manager.request(.POST, "https://www.apiweb.com/apimethod", parameters: parameters, encoding:.JSON)
.response { (request, response, rawdata, error) in
if (response?.statusCode == 200)
{
//DO SOME HEAVY LIFTING
}
}
return result //string
}
另一种异步网络操作的方法
func asyncMethodB() -> String?
{
result : String?
Alamofire.manager.request(.POST, "https://www.yetanotherapiweb.com/apimethod", parameters: parameters, encoding:.JSON)
.response { (request, response, rawdata, error) in
if (response?.statusCode == 200)
{
//DO SOME HEAVY LIFTING
}
}
return result //string
}
我将在其中调用方法 A 和 B 来执行一些操作的方法
func displayResult
{
1) let a = asyncMethodA()
2) let b = asyncMethodB()
3) println(a + b) //some chaotic stuff might happen :(
}
所以问题是我如何才能使 (2) 等待 (1) 运行,(3) 等待 (2) 等等(1,2 和 3 同步运行)?
(我知道一个答案是将 asyncMethodA 和 displayResult 链接到 asyncMethodB 中,但想知道是否还有其他方法)
谢谢你!。