Ethan
Ethan

Reputation: 2087

NSTimer Not Performing Correctly

I am working on an app that requires a lot of user input. Because of this, I am trying to build a feature in that does not allow the user to post more information for at least 10 seconds. This would help to cut down on spammers/trolls/etc. Essentially what happens is when the user want to report something, my application measures the distance between the user and the closest other report location. If they are further than 200 Meters and haven't reported in the last 10 seconds then they are free to post. Here is come code to accomplish this:

if( distance > 200 && canPost){
        MainMap.addAnnotation(pointAnnotation)
        canPost = false
        sendTheData(String(locationManager.location?.coordinate.longitude), latitude: String(locationManager.location?.coordinate.latitude), time: getTime(), userId: "admin")
        let myTimer : NSTimer = NSTimer.scheduledTimerWithTimeInterval(10, target: self, selector: #selector(MapViewController.postDelayTimer), userInfo: nil, repeats: false)
        myTimer.fire()


    }else if(distance < 200 && canPost){
        let alertController = UIAlertController(title: "Whoa There!", message: "tOO clOSE!", preferredStyle: .Alert)
        let defaultAction = UIKit.UIAlertAction(title: "OK", style: .Default, handler: nil)
        alertController.addAction(defaultAction)
        self.presentViewController(alertController, animated: true, completion: nil)


    }

And so on for the other different instances. The function called from the timer, postDelayTimer is this:

func postDelayTimer() {
    canPost = true
}

Pretty simple, but I am just missing something in here obviously. Does anybody have an idea? Thanks all!

What I expected to happen was the user to be unable to post again after immediately posting, but instead they are able to post continuously. It is as if the bool canPost always remains true.

Upvotes: 0

Views: 82

Answers (3)

Ethan
Ethan

Reputation: 2087

I accomplished this by calling the function after the button was pressed.Inside of this function was an NSTimer which, when fired, proceeded to call a third function in charge of charing the required booleans.

Upvotes: 0

Idan
Idan

Reputation: 5450

Two things:

  1. You create the timer inside the a method init the timer as a class variable and use it globally. Something like:

    class MapViewController: UIViewController {
        var myTimer = NSTimer()
    
        func activateTimer() {
          myTimer = NSTimer.scheduledTimerWithTimeInterval(10, target:self, selector: #MapViewController.postDelayTimer), userInfo: nil, repeats: true)
        }
    }
    
  2. myTimer.fire() is unnecessary. The timer will fire once set.

Upvotes: 1

Kurt Revis
Kurt Revis

Reputation: 27994

Your code is:

    let myTimer : NSTimer = NSTimer.scheduledTimerWithTimeInterval(10, target: self, selector: #selector(MapViewController.postDelayTimer), userInfo: nil, repeats: false)
    myTimer.fire()

The second line is incorrect, so remove it. You do not want to manually fire the timer. You want to wait for the timer to fire on its own, after 10 seconds have elapsed.

Upvotes: 1

Related Questions