2

Here is my flow in simple form. I still need to call decisionMaker() when finished after #2 is finished running do to the time is up and take the global variable for the measurements to decisionmaker() for the case test

TestButtonTAPPED() and calls:

  1. recordTimer()
  2. gatherInput()

  3. selector: levelTimerCallback()

  4. decisionMaker()

    // 1.
    func recordTimer() {
        /* After 10 seconds, let's stop the recording process */
        let delayInSeconds = 10.0
        let delayInNanoSeconds = DispatchTime.now() + Double(Int64(delayInSeconds * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)
    
        DispatchQueue.main.after(when: delayInNanoSeconds, execute: {
            self.soundRecorder!.stop()
            self.handBtn.isHidden = false
        })
    
    }
    
    // 2.
    func gatherInput() {
        levelTimer = Timer.scheduledTimer(timeInterval: 0.9, target: self, selector: #selector(DBListener.levelTimerCallback), userInfo:nil, repeats: true)
    }
    
    //3.
    func levelTimerCallback() {
        if soundRecorder.averagePower(forChannel: 0) > -30
        {
            // Do gathering for vaiables
        }
    }
    
    
    // 4.
    func decisionMaker() {
        // case statments here for final measurement
    }
    
    
    
    @IBAction func handTapped(_ sender: UIButton) {
    
        clearGlobalVariablesOnTap()
        listner()
        soundRecorder.record()
        recordTimer()
        dbListener.gatherInput()
    
    }
    
Asdrubal
  • 2,421
  • 4
  • 29
  • 37
james
  • 23
  • 6
  • 1
    I am not sure I understand what you want to achieve :) – Ahmad Baracat Aug 03 '16 at 17:47
  • @Asdrubal I want #2 to call #4 when #2 is finished with its time – james Aug 03 '16 at 19:38
  • Why can't you just call `decisionMaker()` in the if statement in #3 method? – Asdrubal Aug 03 '16 at 19:44
  • @Asdrubal #2 calls #3 several times in the time frame. I need when #2 time is up then to call #4 – james Aug 03 '16 at 19:50
  • couldn't you use something like dispatch_after when you start the call for #2 or use a completion handler when you start the call for #2 – MSU_Bulldog Aug 03 '16 at 19:54
  • @james based on the code you provided above you your timer doesn't stop it just calls levelTimerCallback every 0.9 seconds. So can you provide some code to show when levelTimer stops? Because you have repeat: true so it will just keep repeating and every stop running? If you could provide more code that might help – Asdrubal Aug 03 '16 at 19:54
  • That is how I have it and it stops.? – james Aug 03 '16 at 19:56
  • @MSU_Bulldog http://stackoverflow.com/questions/37801436/how-do-i-write-dispatch-after-gcd-in-swift-3 ... also Go Green? – Asdrubal Aug 03 '16 at 19:58
  • 1
    @MSU_Bulldog that is why I am asking, I am not familiar with the dispatch_after to trailing closure. I looked up close but still confused (lol). I will look up dispatch_after I guess... :) – james Aug 03 '16 at 19:58
  • @james where are you calling `gatherInput()`? – Asdrubal Aug 03 '16 at 20:00
  • @Asdrubal - thanks looking at that post now :) – james Aug 03 '16 at 20:01
  • 1
    @IBAction func handTapped(_ sender: UIButton) { clearGlobalVariablesOnTap() listner() soundRecorder.record() recordTimer() dbListener.gatherInput() } – james Aug 03 '16 at 20:02
  • 1
    @Asdrubal has a good point, your levelTimer is on repeat and if it stops, then you must call levelTimer.invalidate somewhere in your app. If that is the case, then that is the point when you would want to perform the action for #2 being finished.. also mississippi state bulldogs! #HailState – MSU_Bulldog Aug 03 '16 at 20:03
  • @james Please see my answer below and leave a comment if I didn't understand your question correctly. MSU_Bulldog I figured you weren't michigan state based on the bulldog part. But you never know lol – Asdrubal Aug 03 '16 at 20:13

1 Answers1

0
@IBAction func handTapped(_ sender: UIButton) {
    clearGlobalVariablesOnTap()
    listner()
    soundRecorder.record()
    recordTimer()
}

func recordTimer() {

    //As soon as this method is called this portion will get executed
    dbListener.gatherInput()

    /* After 10 seconds, let's stop the recording process */
    let delay = 10.0
    let delayInSeconds = DispatchTime.now() + DispatchTimeInterval.seconds(delay)


    //After 10 seconds this block will execute
    DispatchQueue.main.after(when: delayInSeconds, execute: {
        self.soundRecorder!.stop()
        self.handBtn.isHidden = false
        self.levelTimer.invalidate = true
        self.decisionMaker()
    })

}

func gatherInput() {
    levelTimer = Timer.scheduledTimer(timeInterval: 0.9, target: self, selector: #selector(DBListener.levelTimerCallback), userInfo:nil, repeats: true)
}

func levelTimerCallback() {
    if soundRecorder.averagePower(forChannel: 0) > -30
    {
        // Do gathering for vaiables
    }
}


func decisionMaker() {
    // case statments here for final measurement
}
Asdrubal
  • 2,421
  • 4
  • 29
  • 37