0

I'm creating timer and loop like this.

private var iteration:Int = 0
private var syncTimer:Timer? = Timer()

//MARK: - Singleton
static let synchronizationInstance:DeviceSynchronization = DeviceSynchronization()
private init(){ 
}

public func synchronizeAllDevices(){         
        let when = DispatchTime.now() + 2
        DispatchQueue.main.asyncAfter(deadline: when) {
            self.syncTimer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(DeviceSynchronization.synchronizeDevices), userInfo: nil, repeats: true)
            self.syncTimer?.fire()
        }
    }
}

@objc private func synchronizeDevices(){
    if iteration >= 7 {
        syncTimer?.invalidate()
        syncTimer = nil
        iteration = 0
    } else {
        devicesList![iteration].synchroniseState()
        iteration += 1
    }
}

When it reaches seven reps syncTimer?.invalidate() and syncTimer = nil should stop syncTimer but nothing happens. Timer still works. I don't know here is bug.

shallowThought
  • 19,212
  • 9
  • 65
  • 112
WujoFefer
  • 29
  • 7
  • 1
    You might be called the `synchronizeAllDevices ` again somewhere in your code, `.invalidate()` should work fine – Tj3n Jan 09 '17 at 10:02
  • 1
    As far as I tested your code shown above (I needed to move an unbalanced closing brace), your code worked as expected. As Tj3n suggested, you have some faults in somewhere you are hiding. – OOPer Jan 09 '17 at 10:16
  • I'm not sure about where is the implemention of the class _DeviceSynchronization_, but after the first glance that it seems the `#selector(DeviceSynchronization.synchronizeDevices)` is supposed to be this: `#selector(DeviceSynchronization.synchronizationInstance.synchronizeDevices)` instead; but it is not clear whether this snippet from/in the actual _DeviceSynchronization_ class or not. – holex Jan 09 '17 at 10:24
  • I change #selector(DeviceSynchronization.synchronizationInstance.synchronizeDevices) to #selector(DeviceSynchronization.synchronizationInstance.synchronizeDevices) but it still doesn't work. – WujoFefer Jan 09 '17 at 10:52
  • @WujoFefer, you may need to post the completed implementation to see the whole picture, because in my test class your code works flawlessly and the timer stops after the 7th iteration; but in my test class all of your code (as is) inside the `DeviceSynchronization` class's implementation; but it is not clear at all how that class looks on your screen. – holex Jan 09 '17 at 11:07
  • Show where you call `synchronizeAllDevices`. – shallowThought Jan 09 '17 at 11:17
  • just try syncTimer!.invalidate() – guru Jan 09 '17 at 12:05
  • Possible duplicate of [How Can I Start And Stop NSTimer?](http://stackoverflow.com/questions/12052914/how-can-i-start-and-stop-nstimer) – haider_kazal Jan 09 '17 at 13:05

1 Answers1

0

My sync code start when app connect to server In Stream.Event.openCompleted.

internal func stream(_ aStream: Stream, handle eventCode: Stream.Event) {

    switch eventCode {
    case Stream.Event.openCompleted:
        log.info("The open has completed successfully.")
        reconnectCount = 0
        syncAllDevices.synchronizeAllDevices()
        break
    case Stream.Event.hasSpaceAvailable:
        log.info("The stream can accept bytes for writing.")
        break
    case Stream.Event.hasBytesAvailable:
        receveData()
        break
    case Stream.Event.errorOccurred:
        log.info("An error has occurred on the stream.")
        break
        reconnect()
    case Stream.Event.endEncountered:
        log.info("The end of the stream has been reached.")
        break
    default:
        log.info(„Unknown error”)
        break
    }
}

After 2 seconds I'm trying to sync all devices (objects) in list. I found that this event is triggered two times and I don't know why but this created 2 timers in singleton...

Solved! My problem was when Stream.Event.openCompleted triggered two times thats why my synchronization class creates two timers... I solved the problem by creating a true/false variable "isSyncIsInProgress".

WujoFefer
  • 29
  • 7