1
我遵循this答案給出的建議。還是我的計時器是有時不nil
定時器正在發射兩次,並沒有n
func recreateTimer(){
let timerLog = OSLog(subsystem: "LocationLogger", category: "Timer")
guard shouldUseTimer == true else {
os_log("we are using CoreMotion, no need to set timer", log: timerLog, type: .error)
return
}
if dwellingTimer != nil{
dwellingTimer?.invalidate()
dwellingTimer = nil
}
lastDwelling = lastLocation
os_log("Moved more than 160 | invalidated previous timer began new timer", log: timerLog, type: .error)
DispatchQueue.main.async { [weak self] in
guard self?.dwellingTimer == nil else{
os_log("Timer was not niled!!!", log: timerLog, type: .error)
return
}
self?.dwellingTimer = Timer.scheduledTimer(withTimeInterval: 60, repeats: false, block: { timer in
os_log("we reached 1 minute of no movement",log: timerLog, type: .fault)
self?.locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers
self?.beginMotionTracking()
os_log("Timer is deallocated & invalidated | Accuracy: %{public}f | RemainingTime: %{public}f",log: timerLog, type: .fault,(self?.locationManager.desiredAccuracy)!, UIApplication.shared.remainingTime())
})
}
os_log("New Timer is 'likely' set now", log: timerLog, type: .error)
}
有時候我擊中
os_log("we are using CoreMotion, no need to set timer", log: timerLog, type: .error).
是否有可能在dispatchq塊中產生計時器是問題? – solenoid
你應該在串行調度隊列中同步調度所有更新到你的'dwellingTimer'屬性,以確保操作是線程安全的 – Paulw11
@ Paulw11我一定會這麼做的。我想我需要做的是創建一個串行隊列,並將計時器添加到主runloop或者只是將無效和'nil'ling放入'DispatchQueue.main.async'內......但是我不明白這是怎麼可能從一開始就是一個問題......主線程是一個串行隊列,並且還有'if dwellingTimer!= nil {...}也被串行調用。所以每個部分都應該沒問題... – Honey