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