我只是想知道什么是最好的实施内存优化多功能多计时器在迅速。 定时器是并发并有弱参考与调度? 我试图在一个视图控制器中实现两个定时器,并且出现错误。如何在swift中实现优化多重定时器?
我的计时器之一是这样的:
func startOnPlayingTimer() {
let queue = DispatchQueue(label: "com.app.timer")
onPlayTimer = DispatchSource.makeTimerSource(queue: queue)
onPlayTimer!.scheduleRepeating(deadline: .now(), interval: .seconds(4))
onPlayTimer!.setEventHandler { [weak self] in
print("onPlayTimer has triggered")
}
onPlayTimer!.resume()
}
另一个是:
carouselTimer = Timer.scheduledTimer(timeInterval: 3, target: self,selector: #selector(scrollCarousel), userInfo: nil, repeats: true)
我只会创建多个'Timer'实例。你遇到了什么错误? – Paulw11
@ Paulw11其实,我今天可以用'Timer'实现这个。但我怀疑这个函数线程安全吗?并有一个“弱参考”?我正在寻找与Dispatch实现这一点,并使其线程安全和内存优化。 – Omnia
线程安全和cicrcular强引用是计时器的单独问题。没有计时器方法会给你固有的线程安全。线程的安全性取决于你在由定时器调度的闭包中做什么。定时器可以对能源使用产生影响,事实上苹果公司特别建议不要将定时器用作同步技术; https://developer.apple.com/library/content/documentation/Performance/Conceptual/EnergyGuide-iOS/MinimizeTimerUse.html。您仍然没有解释当您尝试使用两个定时器时出现的“错误”错误 – Paulw11