Alamofire Post请求后,如何设置完成处理程序/阻止? [英] How to have a completion handler/block after Alamofire Post request?

查看:103
本文介绍了Alamofire Post请求后,如何设置完成处理程序/阻止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法来处理 Apple Push Notification Service 远程通知。执行此方法后,我希望它调用服务器并使用 Alamofire 库执行 HTTP POST 请求。我想执行另一种方法来处理POST请求的响应。

I have a method which handles a Apple Push Notification Service remote notification. When this method is executed, I want it to call my server and do a HTTP POST request using the Alamofire library. I want to execute another method that will handle the response of the POST request.

对我来说,问题是我正在使用现有的 API 以在此POST请求中从服务器获取配置文件。因此,我需要使用现有的API并确定何时从远程通知中专门触发了此配置文件获取。

The problem for me is that I am using an existing API to fetch a profile from the server in this POST request. So I need to use this existing API and figure out when this profile fetch is specifically triggered from the remote notification.

由于 Alamofire 请求是在后台队列中完成的,从服务器收到配置文件后,我该如何执行方法的执行?

Since Alamofire requests are done in a background queue, how would I go about doing an execution of a method after receiving the profile back from the server?

谢谢!

推荐答案


由于Alamofire请求是在后台队列中完成的,因此从服务器接收到配置文件后,我该如何执行方法的执行?

Since Alamofire requests are done in a background queue, how would I go about doing an execution of a method after receiving the profile back from the server?

Alamofire内置了响应处理功能。您可以执行以下操作(改编自文档):

Response handling is built in to Alamofire. You can do something like this (adapted from the docs):

Alamofire.request(.POST, "http://httpbin.org/get", parameters: ["foo": "bar"])
         .response { (request, response, data, error) in
                     println(request)
                     println(response)
                     println(error)
                   }

请注意 .response 方法调用,该方法将完成处理程序添加到请求对象;请求完成(或失败)时,Alamofire将调用完成处理程序。

Note the .response method call, which adds a completion handler to the request object; the completion handler is invoked by Alamofire when the request completes (or fails).

这篇关于Alamofire Post请求后,如何设置完成处理程序/阻止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆