0

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)
   }
Danf
  • 1,409
  • 2
  • 21
  • 39
  • you'll need to share a bit more of the context in which you use the timer – Milan Nosáľ Feb 22 '18 at 20:36
  • what kind of context? I instantiate the timer in a tableview func in a switch when the button is taped. all this within: extension wvfDashboardVC: UITableViewDelegate, UITableViewDataSource – Danf Feb 22 '18 at 20:42
  • the code of the view controller.. at least all the parts that are anyhow related to the timer.. – Milan Nosáľ Feb 22 '18 at 20:43
  • I added some code, I apologize for the mess, but I've been trying a lot of things! – Danf Feb 22 '18 at 21:00
  • is that sampling done in the same viewController for all the sensors? – Milan Nosáľ Feb 22 '18 at 21:03
  • at the moment yes... I suppose I could instantiate the viewcontroller somehow? – Danf Feb 22 '18 at 21:05
  • well, the text says otherwise - it is the same code, but two different instances.. show how that controller gets presented – Milan Nosáľ Feb 22 '18 at 21:13
  • Do you mean where the viewDidLoad and vewWillAppear? they are under the class UIViewController, and the buton is a cell in a section in the tableview. Why do you say that there are two instances? – Danf Feb 22 '18 at 21:29
  • based on this: `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.` – Milan Nosáľ Feb 22 '18 at 21:32
  • oh, well, If it were two different instances it wouldn't overwrite the timer... I do that by pressing on the same button two times with a different mac address, but the problem itself is that instead of starting a new instance it overwrites the values of the old, loosing the previous macaddress – Danf Feb 22 '18 at 21:39
  • that's why I want to see the whole code – Milan Nosáľ Feb 22 '18 at 21:40
  • Well, I can't share all of the code because of company policies, and even if I could, it's too much to paste in a snipet! but this view controller comes through a segue from the home screen, were the sensors are connected... – Danf Feb 22 '18 at 22:02
  • add at least `startSampling`, `stopSampling` and `startSync` – Milan Nosáľ Feb 22 '18 at 22:03

0 Answers0