6 Replies - 962 Views - Last Post: 03 August 2016 - 09:50 AM

#1 jaymz69  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 31-July 16

Need help for Swift 2.x - 3.0 on This Program Flow

Posted 31 July 2016 - 10:00 AM

I did not add any code since I was hoping this was better than the code I have scattered..? I have a small app that basically calls some functions when button tapped:
1.    listener() functions fires off after [buttonTapped]:
a.    sets up the UI
b.    calls the animation fuc()

2.    [buttonTapped] then calls:
a.     recorder() func to setup the AV stuff
b.    recordTimer()
i.    recordTimer(): has a timer for 10 seconds

3.    [buttonTapped] then calls:
a.    gatherInput() func:
i.    has a scheduled timer with repeat: true
ii.    a selector that calls levelTimerCallback()

4.    while timer is running(repeating) the levelTimerCallback() on a selctor:
a.    IF statement to record while sound in threshold

5.    *** Now I need to call the decisionMaker() func  ONLY after timer is done with its timed repeat  how can I do that since the timer() already calling a selector to keep running every time the timer() fires of on the repeat?

I have a function that is a timer(repeat) with a selector: calling a function to check sound threshold

ONCE timer is done with the repeat from the time frame

How then to call the decisionMaker() function as I pass the global variable of the average measurement so I can then decide if it is good or bad measurement to call my custom UIAlert

Is This A Good Question/Topic? 0
  • +

Replies To: Need help for Swift 2.x - 3.0 on This Program Flow

#2 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6508
  • View blog
  • Posts: 14,381
  • Joined: 02-June 10

Re: Need help for Swift 2.x - 3.0 on This Program Flow

Posted 31 July 2016 - 01:50 PM

I'm not a big fan of all the timers. Today's languages are pretty much all OOP and event-driven.
Personally I prefer to trigger off events. The device already has a clock on it, right? So subscribe to the time changing event, then count off 10 seconds worth of time changed events. Or at the very least keep only one timer going with a 1000ms interval. Then raise a 'Heartbeat' event every one second. Use that to coordinate all your other events. One of the big problems with timers is they aren't precise. And when you have several running they all start out of sync from each other. If you have 5 timers they will all 'tick' a little out of sync from each other and that makes the UI look odd.

As for the rest of the question... Its just so abstract and generic there's not much anyone can tell you. I mean your only question is "how do I call a function?" - Do you really not know how to call a method/function? I suspect you do, but what you're really asking is "How do I better architect what I'm designing?" and we can't really tackle that for you.
Was This Post Helpful? 0
  • +
  • -

#3 jaymz69  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 31-July 16

Re: Need help for Swift 2.x - 3.0 on This Program Flow

Posted 02 August 2016 - 07:55 AM

I really am just trying to figure the approach to have a timer run for 10 seconds (at least one, the app needs it) then from the timer function I need to pass another function (as I do in the selector:) to make sure that the sound is within threshold to gather samples for the 10 seconds.

ONCE, that original 10 seconds is done I need to call a decisionMaker (that is what I named it) function to now take the samples data that made it past the threshold and the average total for it and pass that global variable to the decisionMaker function to do my case statement (which I have all my cases and custom UIAlerts per one done).

This is a personal app for my first one to get out so I have done everything else just trying to figure out the last step to call my decisionMaker function to run my case.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6508
  • View blog
  • Posts: 14,381
  • Joined: 02-June 10

Re: Need help for Swift 2.x - 3.0 on This Program Flow

Posted 02 August 2016 - 08:14 AM

You seem to have described the need & process. You know what you need to do, when you need to do it, and what conditions to validate.

So I'm not really sure what you're asking use to help you with.
-How do I make a timer?
-How do I call a method?
-How do I pass a parameter?

Maybe I'm just missing the problem beyond a vague logic-flow/design/architecture issue.
Was This Post Helpful? 0
  • +
  • -

#5 jaymz69  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 31-July 16

Re: Need help for Swift 2.x - 3.0 on This Program Flow

Posted 02 August 2016 - 08:34 AM

How to call the 3rd function when timer is over

here is the code:

[code][code]func levelTimerCallback() {

    //update meters before getting the metering values
    soundRecorder.updateMeters()

    var dBValue: Float = 0.0
    let linearValue: Float = pow(10, (0.05 * soundRecorder.averagePower(forChannel: 0)))

    dBValue = soundRecorder.averagePower(forChannel: 0)

    //print to the console if we are beyond a threshold value.
    if soundRecorder.averagePower(forChannel: 0) > -30
    {
        dBValue = 20 * (log10(abs(dBValue)))

        soundTotal = soundTotal + dBValue
        hitTotal = hitTotal + 1

        let homeVC = HomeVC()

        // label print info
        homeVC.dbOutputLbl.text = "\(dBValue)"
        homeVC.dbLinearOutputLbl.text = "\(linearValue)"
        homeVC.averageDbLbl.text = "\(soundTotal)"

        soundTotals = (soundTotal / hitTotal)

        homeVC.dbAvgTotalLbl.text = "\(soundTotals)"
        homeVC.numberOfSampleLbl.text = "\(hitTotal)"

        // DEBUG PRINT info \\\\\\\\\\\\\\\\\\\\\\\
        print("[METHOD START")
        print(" ")
        print("1.1 - dB Value Equivalent: \(abs(dBValue))")
        print(" ")
        print("1.3 - Linear Value Equivalent : \(linearValue)")
        print(" ")
        print("Sound Total: \(soundTotal)")
        print("Total Hits: \(hitTotal)")
        print("-------------------- END METHOD ------------------------")
        print(" ")

    }
}

// timer with selctor call for the time limit 
func gatherInput() {

    //instantiate a timer to be called with whatever frequency we want to grab metering values
    levelTimer = Timer.scheduledTimer(timeInterval: 0.9, target: self, selector: #selector(DBListener.levelTimerCallback), userInfo:nil, repeats: true)

    //================================
    // NOW TIMER IS UP CALL THE desicionMaker()
    //================================
}
[/code][/code]
Was This Post Helpful? 0
  • +
  • -

#6 jaymz69  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 31-July 16

Re: Need help for Swift 2.x - 3.0 on This Program Flow

Posted 03 August 2016 - 09:48 AM

**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
    }
    
}


Was This Post Helpful? 0
  • +
  • -

#7 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13568
  • View blog
  • Posts: 54,141
  • Joined: 12-June 08

Re: Need help for Swift 2.x - 3.0 on This Program Flow

Posted 03 August 2016 - 09:50 AM

What is the question?
---
Don't make duplicate topics. Merging.

This post has been edited by modi123_1: 03 August 2016 - 09:52 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1