0

I'm working in swift and want to run a function every minute. I want to update a label with a count down timer with how many minutes left till the next update.

I have a basic version working

    if let date = newDate {
        let formatter : NSDateFormatter = NSDateFormatter()
        formatter.dateFormat = "HH:mm"
        formatter.timeZone = NSTimeZone.defaultTimeZone()

        let string : NSString = formatter.stringFromDate(date)

        let calendar = NSCalendar.currentCalendar()
        let comp = calendar.components([.Minute], fromDate: date)
        let minute = comp.minute

        let remaining : Int = 60 - minute
        var mins = "s"
        if remaining == 1 {
            mins = ""
        }

        self.refreshInLabel.text = "Refreshes at \(string) - \n \(remaining) minute\(mins) remaining "
    }

which is updating when i view the page on the app, but i want it to auto update every minutes. I've looked at NSTimer, i believe it can be done with this (as shown here: How to make a countdown with NSTimer on Swift) but i can't work out how to make it fire on the minute, only after a certain time display

Edit: I have the following so far

override func viewDidLoad() {
    super.viewDidLoad()

    let calendar = NSCalendar.currentCalendar()
    let comp = calendar.components([.Minute], fromDate: NSDate())
    minute = comp.minute

    _ = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(AdoptionCentreVC.updateTimer), userInfo: nil, repeats: true)

}

func updateTimer() {
    let calendar = NSCalendar.currentCalendar()
    let comp = calendar.components([.Minute], fromDate: NSDate())
    let curMin = comp.minute

    if(curMin > minute) {
        NSLog("Changed")
        self.minute = curMin
    }
}

I'd like to know if theres a better way

Community
  • 1
  • 1
Ceri Turner
  • 830
  • 2
  • 12
  • 36

2 Answers2

0

What I suggest you is to fire a NSNotification when you want to start your func every minute. When you receive your NSNotification call a function like this :

var yourTimer = NSTimer()
func callWhenNotificationReceived(){
  yourFuncToFire()
  yourTimer = NSTimer.scheduledTimerWithTimeInterval(60, target: self, selector: #selector(YourViewController.yourFuncToFire) , userInfo: nil, repeats: true)
}
Chajmz
  • 729
  • 5
  • 11
-1
var TotalTime:Int = 0
var timer: NSTimer?


triggerCountDownTimerFor(time:Int)

func triggerCountDownTimerFor(time:Int)
{
   totalTime = time
    timer = NSTimer.scheduledTimerWithTimeInterval(1, target:self, selector: #selector(LoginViewController.updateTimer), userInfo: nil, repeats: true)
}

func updateTimer()
{
    let date = NSDate()
    let formatter : NSDateFormatter = NSDateFormatter()
    formatter.dateFormat = "HH:mm"
    formatter.timeZone = NSTimeZone.defaultTimeZone()

    let string : NSString = formatter.stringFromDate(date)

    let calendar = NSCalendar.currentCalendar()
    let comp = calendar.components([.Minute], fromDate: date)
    let minute = comp.minute

    let remaining : Int = TotalTime - minute
    var mins = "s"
    if remaining == 1 {
        mins = ""
    }

    print("Refreshes at \(string) - \n \(remaining) minute\(mins) remaining ")




}
Sucharu Hasija
  • 1,096
  • 11
  • 23