So my problem looks like this: I am working on an app to keep track of some data-loggers. I am an electronic engineer so I new to swift.
In a tableview I list all connected devices, and when I tap in one cell, it takes me to another tableview with some configuration data of the corresponding sensor.
Here, I have a button cell to capture data. Once I tap on it, it starts the capture sequence and sets up a timer ,
timer = Timer.scheduledTimer(timeInterval: 30, target: self, selector:#selector(ViewController.timerHandler), repeats: false)
After 30 seconds the handler is called and it syncs and retrieve the data from the sensor.
Until here everything works perfectly. All commands for the sensors take the mac address as argument.
The problem occurs when I try to take measurements from two devices at once.
I enter the view controller with the mac address from the first device and it starts it's capture, then I enter the view controller again with the mac address of the second device and I press capture, and ir overwrites the timer and starts it's capture, but the first device is lost.
I think I have to use userInfo for the timers, where the userinfo would be the macaddress, but I don't know how to instantiate the timer without overwriting it every time.
I'd appreciate any help!
------ EDIT -------
I add some of the code where the timer is:
func tableView(_ tableView: UITableView, didSelectRowAtindexPath:IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
switch (indexPath.section) {
case 0:
switch (indexPath.row) {
case 0: self.newName()
case 3: self.performSegue(withIdentifier: "sessionSegue", sender: self)
default: break
}
case 1:
switch (indexPath.row) {
case 0:
startSampling(self.macAddress)
timer = Timer.scheduledTimer(timeInterval: 30, target:self, selector: #selector(wvfDashboardVC.timerhandler), repeats: false)
default: break
}
default: break
}
}
And the handler:
func timerhandler(){
stopSampling(self.macaddress)
startSync(self.macaddress)
}