你好
我希望在后台每60秒执行一次以下代码,以检查数据库和服务器是否有更新:
let request = NSMutableURLRequest(URL: NSURL(string: "http://.........")!)
request.HTTPMethod = "POST"
let postString = "cardnumber=\(txtCardNamber.text)&mobile=\(txtMobileNamber.text)"
//let postString = "cardnumber=\(txtCardNamber.text)&mobile=\(txtMobileNamber.text)"
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if error != nil {
print("error=\(error)")
return
}
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
let asr = responseString
print("Holol ID = \(asr)")
//var asrlen = asr?.length
//var ss = asr!.substringWithRange(NSRange(location: 0, length: asrlen!))
}
task.resume()
由于
答案 0 :(得分:0)
假设您将该代码放入名为" update
"的函数中,您可以使用NSTimer:
timer = NSTimer.scheduledTimerWithTimeInterval(60.0, target: self, selector: "update", userInfo: nil, repeats: true)
(其中timer
是一个强类属性,因此ARC在您设置之后不会很乐意释放它。)
Here is a tutorial可能有所帮助。