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.