2017-08-08 46 views
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). 
+0

是否有可能在dispatchq块中产生计时器是问题? – solenoid

+1

你应该在串行调度队列中同步调度所有更新到你的'dwellingTimer'属性,以确保操作是线程安全的 – Paulw11

+0

@ Paulw11我一定会这么做的。我想我需要做的是创建一个串行队列,并将计时器添加到主runloop或者只是将无效和'nil'ling放入'DispatchQueue.main.async'内......但是我不明白这是怎么可能从一开始就是一个问题......主线程是一个串行队列,并且还有'if dwellingTimer!= nil {...}也被串行调用。所以每个部分都应该没问题... – Honey

回答

0

由于电磁阀和Paulw11意见,我了解到,相关的计时器所有执行应该做一种连续的方式。

另见invalidate

您必须从该定时器是 安装线程发送此消息。如果您从另一个线程发送此消息,则与定时器关联的输入源 可能不会从其运行循环 中删除,这可能会阻止该线程正常退出。

所以除了invalidatingnilling(你不想给非nil的对象不再传达任何意义),请确保您:

  1. 使用相同的线程
  2. 您正在使用的线程是串行的
  3. 取决于您对定时器进行更改的位置,您可能需要也可能不需要同步。