【发布时间】:2018-01-16 13:12:12
【问题描述】:
我正在遵循this 答案中给出的建议。我的计时器仍然是 sometimes not nil
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 块中生成计时器是否可能是问题?
-
你应该在串行调度队列上同步调度你的
dwellingTimer属性的所有更新,以确保操作是线程安全的 -
@Paulw11 我一定会这样做的。我想我需要做的是创建一个串行队列并将计时器添加到主运行循环,或者只是将失效和
nilling 放入DispatchQueue.main.async... 但是我不明白这怎么可能从一开始就是一个问题......主线程是一个串行队列,并且if dwellingTimer != nil{...行也以串行方式调用。所以每个部分都应该没问题... -
@Paulw11 实际上让我们假设我收到 3 个超快速调用,niled、niled、niled、scheduled、scheduled、scheduled(即我不会得到 niled、scheduled、niled、scheduled、niled 计划)- -> 所以我得到 3 个计时器而不是 1 个?!嗯,如果这就是你的意思,那么即使
dispatch.main.async也无济于事,因为它不是你所说的synchronously,我必须手动完成,即创建线程添加计时器到运行循环......请纠正我曾经在哪里错了 -
是的,对属性的访问可能不是原子的,并且异步调度将在当前函数返回后的某个时间发生。如果您在原子操作中更新属性(并在必要时安排计时器)(如果需要,通过 dispatch_sync),那么您将防止这种情况发生
标签: ios swift multithreading timer nstimer