I have actions that must continue until the switch that initiated them is changed. I have tried to use GCD with a Work Item queue believing that the async action would allow the user to change the switch and consequently stop the actions. When the code runs it never sees or even triggers a switch change. Here is my code. Any idea what I am doing wrong?
@IBAction func trailerScanSwitchChange(_ sender: Any) {
let model = Model() //Instantiate the Model
gScan = trailerScanSwitch.isOn
//Set up work iten to read and parse data
let work1 = DispatchWorkItem {
while (gScan && gConnected){
model.parseValues()
print ("parsing values")
if gScan == false || gConnected == false{
break
}
model.convertVoltsToLights()
self.updateLights()
print ("updating Lights")
if gScan == false || gConnected == false{
break
}
}
}
if trailerScanSwitch.isOn == true{
print ("Scanning")
//perform on the current thread
work1.perform()
//perpform on the global queue
DispatchQueue.global(qos: .background).async(execute: work1) // concurrent actions
return
}
else { //Stop reading and displaying
gScan = false
work1.cancel()
}
}