I have implemented an Operation on an OperationQueue.
override func main() {
super.main()
if isCancelled {
return
}
if member.memberType == .timed {
triggerRestEvent(duration: member.restDuration)
}
if isCancelled {
triggerEndEvent()
}
}
The triggerRestEvent function is actually calling Thread.sleep. Once the sleep has expired we then check isCancelled.
Is there a way to interrupt Thread.sleep when isCancelled is toggled on?
Alternative - RunLoop
The docs for RunLoop suggest a while loop around the function run with a custom condition in the while loop. But how would I setup a timer to toggle the while loops execution? And apparently using a while loop in this way, for this purpose, is an antipattern these days?