大佬教程收集整理的这篇文章主要介绍了swift – 在块中调用时,NSTimer不会触发,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
func startTimer () { batchTimer = NSTimer.scheduledTimerWithTimeInterval(batchIntervalSeconds,target: self,SELEctor: #SELEctor(requester.performRemoterequests),userInfo: nil,repeats: falsE) }
事实并非如此
func startTimerInBlock () { let urlrequest = NSMutableURLrequest(URL: NSURL(String: "google.com")!,cachePolicy: .ReloadIgnoringLocalCacheData,timeoutInterval: 30) urlrequest.httpR_500_11845@ethod = "GET" let session = NSURLSession(configuration: NSURLSessionConfiguration.ephemeralSessionConfiguration()) let task = session.dataTaskWithrequest(urlrequest) { (data:NSData?,response:NSURLResponse?,error:NSError?) -> Void in //check there is a response and that it is an http response self.batchTimer = NSTimer.scheduledTimerWithTimeInterval(self.batchIntervalSeconds,SELEctor: #SELEctor(CNVRrequester.performRemoterequests),repeats: falsE) } task.resume() }
DispatchQueue.main.async { self.startTimer() }
那应该解决它.
编辑说明
计时器需要一个活动的运行循环.在主线程上初始化时,将自动使用主运行循环.如果要从后台线程运行它,则必须将其附加到该线程运行循环.例
DispatchQueue.global(qos: .BACkground).async { let timer = Timer.scheduledTimer(timeInterval: 10,SELEctor: SELEctor(fireTimer),repeats: falsE) let runLoop = RunLoop.current runLoop.add(timer,forMode: .defaultRunLoopModE) runLoop.run() }
但是,如果你想确保它只是从主线程运行,只需从一个调度主关闭启动它,它将确保它将运行主线程.
编辑:已更新为Swift 3
以上是大佬教程为你收集整理的swift – 在块中调用时,NSTimer不会触发全部内容,希望文章能够帮你解决swift – 在块中调用时,NSTimer不会触发所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。