1

I am calling Web service. In that method session.dataTaskWithURL is calling after some time.I think it is not giving in proper thread?

my code below
——--------------

 func callService(usr: String, pwdCode: String) ->Bool
{
var resultPage=false
let url = NSURL(string: "demourl")
var xmlParse:NSString = ""
var data : NSData!

println("Before request *****************")

let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: config)

session.dataTaskWithURL(url!,
completionHandler: {(data, response, error) in

let request = NSMutableURLRequest(URL: url!)
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.HTTPMethod = "POST"
let dictionary = ["email": usr, "userPwd": pwdCode] as NSDictionary
var error: NSError?

if let body = NSJSONSerialization.dataWithJSONObject(dictionary, options: nil, error: &error) {
request.HTTPBody = body
} 
else
{
    println("JSON error: \(error)")
}

let xmlParse=NSString(data: data, encoding: NSUTF8StringEncoding)!

if data == nil {
    println("dataTaskWithRequest error: \(error)")
return
}

let parser = NSXMLParser(data: data)
parser.delegate = self
resultPage=parser.parse()

println("******** boolVal \(resultPage)")
}).resume()

println("After request *****************")
println("resultPage Final \(resultPage)")
return resultPage;
}

Can you please help me if we can execute my method "session.dataTaskWithURL(url!, completionHandler: {(data, response, error) in " in a proper sequence??? Thanks in advance.

Cœur
  • 37,241
  • 25
  • 195
  • 267
dhaval shah
  • 4,499
  • 10
  • 29
  • 42
  • There's nothing wrong with your code. You just cannot return a value that way. The completion block gets executed some time in future. To do what you want to do you need to pass in a block to the `callService` function and call that at the end of your completion block. Once you go with blocks you never can go back ;) (actually there is a way but it's blocking and that's not good) – HAS Dec 01 '14 at 08:18
  • thanks for the suggestions..but i didnt get you how you want me to put in block to the call service??Can u please amend my code and brief me??? – dhaval shah Dec 01 '14 at 09:41
  • I'm actually a little bit confused by your code ... In your completion block you're creating another NSURLRequest. What do want to do with it? I think you want to create that *before* you create the data task and initialize this data task with the created NSURLRequest. What do you want do? Do you want to post something and then parse a response data which is in XML? – HAS Dec 01 '14 at 09:54
  • 1
    Sorry was trying the wronge one..i have updated my answer below now..many thanks for ur help HAS.. – dhaval shah Dec 01 '14 at 11:24

1 Answers1

1
    var resultPage=false
    let url = NSURL(string: "URL")
    var xmlParse:NSString  = ""
    var data : NSData!

    let request = NSMutableURLRequest(URL: url!)
    request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
    request.HTTPMethod = "POST"
    let dictionary = ["email": base64String, "userPwd": base64StringPwd] as NSDictionary
    var error: NSError?
    if let body = NSJSONSerialization.dataWithJSONObject(dictionary, options: nil, error: &error)        {
        request.HTTPBody = body
    } else {
        println("JSON error: \(error)")
    }

    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
    (data, response, error) in
    println(NSString(data: data, encoding: NSUTF8StringEncoding))
    let xmlParse=NSString(data: data, encoding: NSUTF8StringEncoding)!

    if data == nil {
    println("dataTaskWithRequest error: \(error)")
    return
    }

    let parser = NSXMLParser(data: data)


    println("parser \(parser)")
    parser.delegate = self
    resultPage=parser.parse()


    if self.success==false
    {

        println("success \(self.success)")
        self.lblMessage.hidden=false
    }

}
    task.resume()
dhaval shah
  • 4,499
  • 10
  • 29
  • 42