I have an "AFK" detector in my app. However it's behaving very strangely and I really don't know why. Note I have other timers in the app however they have their own variable names and I don't cross-them at any time. I think they're all independent unless they all somehow run on the same thread or some "under-the-hood" shenanigans happens. Here is the code that manages it:
func resetRoundtimer() {
roundTime = 75
}
@objc func updateRoundTimer() {
if roundTime < 0.5 {
print ("Round time is up!")
timeRemaining.invalidate()
skipTurnDueToInactivity()
timerActivity.stopAnimating()
skipTurn = true
} else {
roundTime -= 0.5
}
timerLabel.text = String(format: "%.01f", roundTime)
}
func startRoundTimer() {
timeRemaining = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(self.updateRoundTimer), userInfo: nil, repeats: true)
timerActivity.startAnimating()
}
This triggers "Skip due to inactivity" which is another function that also counts how many times you've been AFK and if there are 3 you should be disconnected for being AFK too long:
func skipTurnDueToInactivity() {
timeRemaining.invalidate()
print ("YOU HAVE BEEN AFK LONG ENOUGH! SKIPPING TURN! \(afkSkips)")
if afkSkips == 3 {
print ("YOU HAVE BEEN AFK 3 TIMES, DISCONNECTING YOU FROM MATCH DUE TO INACTIVITY")
print("You ded son!")
sendData(key: DID_ELIMINATE, stage: currentStage, int: nil, double: nil, player: nil, notes: nil)
didEliminatePlayer(playerDed: GKLocalPlayer.localPlayer())
} else {
afkSkips += 1
}
}
This is the console output:
Round time is up!
YOU HAVE BEEN AFK LONG ENOUGH! SKIPPING TURN! 0
AFK hostQ
Round time is up!
YOU HAVE BEEN AFK LONG ENOUGH! SKIPPING TURN! 1
AFK hostQ
Round time is up!
YOU HAVE BEEN AFK LONG ENOUGH! SKIPPING TURN! 2
AFK hostQ
Round time is up!
YOU HAVE BEEN AFK LONG ENOUGH! SKIPPING TURN! 3
YOU HAVE BEEN AFK 3 TIMES, DISCONNECTING YOU FROM MATCH DUE TO INACTIVITY
You ded son!
if I clearly "invalidated" the timer why does it keep firing the skipTurn function?