我有這樣一類的asynchronus請求,如何調用上asynchronus請求完成的功能 - 雨燕2.0
class Req{
func processRequest(){
NSURLConnection.sendAsynchronousRequest(request, queue: queue, completionHandler:{ (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
//Do some operation here
})
}
}
我打電話來,從這樣一個視圖控制器,這種方法
class classOne{
func classOneFun(){
Req().processRequest()
self.demoFun()
}
func demoFun(){
//Do some operation here
}
}
而且我打電話這樣從另一個視圖控制器相同的功能,
class classTwo{
func classTwoFun(){
Req().processRequest()
self.sampleFun()
}
func sampleFun(){
//Do some operation here
}
}
現在我只想在完成processRequest()
之後致電demoFun()
或sampleFun()
。如果demoFun() or sampleFun()
和processRequest()
在同一個類中,那麼我可以在processRequest()的完成處理程序中調用這些函數。但是,在我的情況下,我不能這樣做,因爲我有近20個視圖控制器調用processRequest()函數。我無法使用SynchronusRequest,因爲它在Swift 2.0中不推薦使用。那麼一旦異步請求完成,我怎麼能調用其他函數呢?
感謝您的答覆。我沒有使用'callback',我不知道它是什麼。我可以知道它是如何工作的嗎? –
或者如果你有任何關於這個的教程,你可以在這裏分享嗎? –
回調是參數的名稱。將callBack替換爲「closureThatIsGoingToBeCalledWhenTheRequestSucceeds」,看看它是否更有意義。 – gnasher729