我有一个方法来处理 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, "httpbin/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请求后,如何设置完成处理程序/阻止?
发布评论