我只是想知道什麼是最好的實施內存優化多功能多計時器在迅速。 定時器是併發並有弱參考與調度? 我試圖在一個視圖控制器中實現兩個定時器,並且出現錯誤。如何在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