How to get completion handler / block after Alamofire Post request?

I have a method that handles a remote notification Apple Push Notification Service

. When this method is executed, I want it to call my server and execute the request HTTP POST

using the library Alamofire

. I want to execute another method that will handle the response of the POST request.

The problem for me is that I am using an existing one API

to fetch the profile from the server in this POST request. So I need to use this existing API and figure out exactly when this profile selection is called from the remote notification.

Since the requests Alamofire

are done in the background, how can I execute the method after getting the profile from the server?

What would be a good option for solving this problem?

Thank!

+3


source to share


3 answers


Since Alamofire requests are executed in the background, how can I execute the method after receiving the profile from the server?

Response handling is built into 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)
                   }

      

Notice the method call .response

that adds a completion handler to the request object; a completion handler is called by Alamofire when a request completes (or fails).

+4


source


It is not clear from your wording of the question what problem you are trying to solve. But you have clarified your intention in the comments above.

As I understand it now, you have code that updates the profile on the server and handles the server's response. The code is called in two contexts, one is manually initiated by a user request, the other is initiated by a push notification. In the first case, you don't want to generate an alert after processing the response from the server, but in the second case, you do.

You do have a closure that you can use to handle different behavior, even if the difference is in the asynchronous part of the process. Here is a sketch (not the actual working code) of how it might look:



func updateProfile(parameters: [String:String], showAlert: Bool) {
    Alamofire.request(.POST, "http://myserver.com/profile", parameters: parameters)
             .response { (request, response, data, error) in
                         if (error == nil) {
                           processProfileResponse(response)
                           if showAlert {
                             showProfileWasUpdatedAlert()
                           }
                         }
                   }      

}

      

Pay attention to the parameter showAlert

passed to the method updateProfile

. If you go to true

, it calls a method showProfileWasUpdatedAlert

to display your warning after receiving a server response. Note that this boolean is "captured" by the closure, which handles Alamofire's response, because the closure was defined inside the function updateProfile

.

This, IMHO, is a better approach than declaring a global application inside your AppDelegate.

+1


source


Here you go

func AlamofireRequest(method: Alamofire.Method, URLString: URLStringConvertible, parameters: [String : AnyObject]?, encoding: ParameterEncoding, headers: [String : String]?) -> Alamofire.Result<String>? {
    var finishFlag = 0
    var AlamofireResult: Alamofire.Result<String>? = nil
    Alamofire.request(method, URLString, parameters: parameters, encoding: encoding, headers: headers)
        .responseString { (_, _, result) -> Void in
            if result.isSuccess {
                finishFlag = 1
                AlamofireResult = result
            }
            else {
                finishFlag = -1
            }
    }
    while finishFlag == 0 {
        NSRunLoop.currentRunLoop().runMode(NSDefaultRunLoopMode, beforeDate: NSDate.distantFuture())
    }
    return AlamofireResult
}

      

+1


source







All Articles