-1

**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()
 2a. selector: levelTimerCallback()
 
3. 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)
}


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


// 3.
func decisionMaker() {
    // case statments here for final measurement
}

}

Community
  • 1
  • 1
iOS Newbie
  • 117
  • 1
  • 8

1 Answers1

0

I may have a solution if I get the idea right.

 var callbacks: [() -> ()] = []

 var levelTimerCallback = {
     //some code here...
 }

 var decisionMaker = {
     //other code here...
 }

 (1...9).forEach() { _ in
     callbacks.append(levelTimerCallback)
 }

 callbacks.append(decisionMaker)

 func timerCallback() {

     let operation = callbacks.removeFirst()
     operation()
 }

 var levelTimer = Timer.scheduledTimer(timeInterval: 0.9, target: self, selector:   #selector(timerCallback), userInfo:nil, repeats: true)

So this would call the levelTimer 9 times and the decisionMaker at the end. Although I'm still not sure, that this is what you are looking for.

gujci
  • 1,238
  • 13
  • 21
  • Let me test that, that is what I am trying to do in a nut shell. What you call the way you declare the callbacks: [() -> ()] = [] ? – iOS Newbie Aug 02 '16 at 19:11
  • [() -> ()] = [] this declares an array of functions that does not have parameters and return values. So you can add all the necessary callbacks to it. – gujci Aug 02 '16 at 20:20