1

Web サービスを呼び出しています。そのメソッドでは、session.dataTaskWithURL がしばらくしてから呼び出されています。適切なスレッドで提供されていないと思いますか?

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;
}

私のメソッド "session.dataTaskWithURL(url!, completionHandler: {(data, response, error) in " を適切な順序で実行できるかどうか助けてください。よろしくお願いします。

4

1 に答える 1

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()
于 2014-12-01T11:23:52.367 に答える