iosswiftpostnsurlconnectionsendasynchronousrequest

Returning value from NSURLConnect.SendAsyncronousRequest


func postToServerAction() -> String{
   var stat = ""

    var url: NSURL = NSURL(string: /*External LinK */)!

    var request:NSMutableURLRequest = NSMutableURLRequest(URL:url)

    var bodyData = "Username=" + userTxt.text + "&Password=" + passwordTxt.text

    //var bodyData = "Username=" + userTxt.text
    request.HTTPMethod = "POST"
    request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding);

    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue())

        {
            (response, data, error) in

            var reciveData = NSString(data:data,encoding: NSUTF8StringEncoding)

            if(reciveData == "false")
            {
               stat = "YES"
            }else{
               stat = "NO"
            }
        }


    return stat



}

Here some Code

I would like to return my variable but there no value inside. may be it is problem on NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) can anyone tell me how can I manage this func. I would like to return the value from NSURLConnection.sendAsynchronousRequest

in my case there no return value and my variable "reviceData" give me correct value but I still cant assign it to any variable

I'm new for swift and not good in English sorry for the gramma.


Solution

  • You are calling an async task but it looks like you think its not async. You are returning stat in an async code block but you seem to think that its executed before your return statment is hit, this is not so and you should read up on how threading works. When you return stat, stat doesn't exist yet because that was passed on to another thread that will take an unknown time to complete. Therefore, you need to pass in a callback to fire when stat exists which is inside your async task. See below how I edited your code (callbackToFire needs to be created and passed in by you).

    func postToServerAction(callbackToFire:(String)->Void) -> Void{
           var stat = ""
    
            var url: NSURL = NSURL(string: /*External LinK */)!
    
            var request:NSMutableURLRequest = NSMutableURLRequest(URL:url)
    
            var bodyData = "Username=" + userTxt.text + "&Password=" + passwordTxt.text
    
            //var bodyData = "Username=" + userTxt.text
            request.HTTPMethod = "POST"
            request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding);
    
            NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue())
    
                {
                    (response, data, error) in
    
                    var reciveData = NSString(data:data,encoding: NSUTF8StringEncoding)
    
                    if(reciveData == "false")
                    {
                       stat = "YES"
                    }else{
                       stat = "NO"
                    }
    
                    callbackToFire(stat)
                }
        }