关闭主线程执行POST

时间:2015-03-23 03:15:33

标签: ios xcode swift

如何在主线程下面执行帖子?

  var post:String = "fname=\(addy)&lname=\(fff)&time=\(ff)&action=\(sd_asds)&actionid=add&content=\(d)".stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!

是否可能在主线程上运行帖子会导致内存警告?

1 个答案:

答案 0 :(得分:0)

为了发送POST请求,您可以使用NSURLConnection类,该类具有发送请求asyncrhonously的方法,这意味着它将执行"关闭主线程"。

示例:

 var post:String = "fname=\(addy)&lname=\(fff)&time=\(ff)&action=\(sd_asds)&actionid=add&content=\(d)".stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
 var postURL: NSURL = NSURL(string: post)

let request: NSURLRequest = NSURLRequest(URL:postURL)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler:{ response, data, error in 
//your code here
 })

<强>文档

https://developer.apple.com/library/prerelease/ios/documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/index.html#//apple_ref/occ/clm/NSURLConnection/sendAsynchronousRequest:queue:completionHandler