Ios 如何在异步请求完成时调用函数-Swift 2.0
我在这样的类中有一个异步请求Ios 如何在异步请求完成时调用函数-Swift 2.0,ios,swift,swift2,sendasynchronousrequest,Ios,Swift,Swift2,Sendasynchronousrequest,我在这样的类中有一个异步请求 class Req{ func processRequest(){ NSURLConnection.sendAsynchronousRequest(request, queue: queue, completionHandler:{ (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in //Do some operation
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
}
}
我从一个视图控制器调用这个方法
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
}
}
我从另一个视图控制器调用相同的函数
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()或sampleFun()
和processRequest()
在同一个类中,那么我可以调用processRequest()的完成处理程序中的函数。但是,在我的例子中,我不能这样做,因为我有将近20个视图控制器调用processRequest()函数。我不能使用SynchronousRequest,因为它在Swift 2.0中已被弃用。那么,在异步请求完成后,如何调用其他函数呢?您需要修改processRequest函数以接收闭包。例如:
class Req{
func processRequest(callback: Void ->Void){
NSURLConnection.sendAsynchronousRequest(request, queue: queue, completionHandler:{ (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
//Do some operation here
//Call the callback here:
callback()
})
}
}
class classOne{
func classOneFun(){
Req().processRequest(){ //Passing the closure as a trailing closure with the code that we want to execute after asynchronous processing in processRequest
self.demoFun()
}
}
func demoFun(){
//Do some operation here
}
}
现在,无论您想在何处调用ProcessRequestAPI,都可以添加希望在异步回调处理之后立即执行的代码。例如:
class Req{
func processRequest(callback: Void ->Void){
NSURLConnection.sendAsynchronousRequest(request, queue: queue, completionHandler:{ (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
//Do some operation here
//Call the callback here:
callback()
})
}
}
class classOne{
func classOneFun(){
Req().processRequest(){ //Passing the closure as a trailing closure with the code that we want to execute after asynchronous processing in processRequest
self.demoFun()
}
}
func demoFun(){
//Do some operation here
}
}
HTH.创建块
class Req{
func processRequest(success: () -> ()){
NSURLConnection.sendAsynchronousRequest(request, queue: queue, completionHandler:{ (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
//Do some operation here
success()
})
}
}
class classOne{
func classOneFun(){
Req().processRequest { () -> () in
self.demoFun()
}
}
func demoFun(){
//Do some operation here
}
}
谢谢你的回复。我没有使用过
回调
,也不知道它是什么。我可以知道它是如何工作的吗?或者如果你有关于这个的教程,你能在这里分享吗?callback是一个参数的名称。将callBack替换为“请求成功时将被调用的closure”,看看它是否更有意义。在苹果的两本Swift书中,你到底发现了什么?他们详细地解释了一切。当然。。非常感谢你的回复。如果我能得到一些关于闭包或回调函数的教程,那将对我有所帮助..你可以在这里查看官方文档:如果没有足够的时间,请阅读以下内容:--\