我是初学程序员(swift -ios 8) 如何刷新我在后台(每5秒)的功能在swift中 我究竟需要什么?
由于
答案 0 :(得分:1)
我认为你需要这个:
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { () -> Void in
//add your function here which will run in background like fetching data from server
})
修改强>
如果您想以固定的时间间隔运行任何功能,可以这样使用NSTimer
:
var timer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: Selector("yourMethod"), userInfo: nil, repeats: true)
并添加其辅助方法:
//this will repeat on every 5 second
func yourMethod(){
//your code
}
答案 1 :(得分:-1)
func testtcpclient(){
var client:TCPClient = TCPClient(addr:"46.143.211.41", port:8080)
var (success,errmsg)=client.connect(timeout: 1)
if success{
var (success,errmsg)=client.send(str:"AEBCD1" )
if success{
var data=client.read(1024*10)
if let d=data{
if let str=String(bytes: d, encoding: NSUTF8StringEncoding){
println(str)
}
}
}else{
println(errmsg)
var alert = UIAlertView()
alert.title = "no"
alert.message = "no"
alert.addButtonWithTitle("ok")
alert.show()
}
}else{
println(errmsg)
var alert = UIAlertView()
alert.title = "no"
alert.message = "no"
alert.addButtonWithTitle("ok")
alert.show()
}
}
var timer = NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: Selector("testtcpclient"), userInfo: nil, repeats: true)
}