WujoFefer 1 weeks ago

I can not stop Timer Swift 3

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.

Answers


Nirmalsinh 1 weeks ago

You can use below code:

myTimer?.invalidate()
myTimer = nil

Post Status

Asked in 1 weeks ago
Viewed 3,998 times
Voted 9
Answered 1 times

Search




Leave an answer