How can I make a countdown with NSTimer?

In Swift 5.1 this will work: var counter = 30 override func viewDidLoad() { super.viewDidLoad() Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(updateCounter), userInfo: nil, repeats: true) } @objc func updateCounter() { //example functionality if counter > 0 { print(“\(counter) seconds to the end of the world”) counter -= 1 } }

How do I create a NSTimer on a background thread?

If you need this so timers still run when you scroll your views (or maps), you need to schedule them on different run loop mode. Replace your current timer: [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES]; With this one: NSTimer *timer = [NSTimer timerWithTimeInterval:0.5 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES]; [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes]; For details, check this … Read more

What’s the best way to detect when the app is entering the background for my view?

You can have any class interested in when the app goes into the background receive notifications. This is a good alternative to coupling these classes with the AppDelegate. When initializing said classes: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillResignActive:) name:UIApplicationWillResignActiveNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillTerminate:) name:UIApplicationWillTerminateNotification object:nil]; Responding to the notifications -(void)appWillResignActive:(NSNotification*)note { } -(void)appWillTerminate:(NSNotification*)note { [[NSNotificationCenter defaultCenter] … Read more

How to run a method every X seconds

The solution you will use really depends on how long you need to wait between each execution of your function. If you are waiting for longer than 10 minutes, I would suggest using AlarmManager. // Some time when you want to run Date when = new Date(System.currentTimeMillis()); try { Intent someIntent = new Intent(someContext, MyReceiver.class); … Read more

How can I use Timer (formerly NSTimer) in Swift?

This will work: override func viewDidLoad() { super.viewDidLoad() // Swift block syntax (iOS 10+) let timer = Timer(timeInterval: 0.4, repeats: true) { _ in print(“Done!”) } // Swift >=3 selector syntax let timer = Timer.scheduledTimer(timeInterval: 0.4, target: self, selector: #selector(self.update), userInfo: nil, repeats: true) // Swift 2.2 selector syntax let timer = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, … Read more

How do I use NSTimer?

Firstly I’d like to draw your attention to the Cocoa/CF documentation (which is always a great first port of call). The Apple docs have a section at the top of each reference article called “Companion Guides”, which lists guides for the topic being documented (if any exist). For example, with NSTimer, the documentation lists two … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)