james james - 3 months ago 8
Swift Question

How to call a function within the function below after the first has done from a timer

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()


    }


Answer
@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 delayInSeconds = 10.0
    let delayInNanoSeconds = DispatchTime.now() + Double(Int64(delayInSeconds * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)


    //After 10 seconds this block will execute
    DispatchQueue.main.after(when: delayInNanoSeconds, 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
}
Comments