如何在选择器成功之前调用NSTimer

时间:2016-11-20 07:13:24

标签: ios swift parse-platform nstimer parse-server

我有一个名为" Check"检查对象是否已更新,如果是这种情况,则将用户发送到另一个视图控制器。但是,NSTimer不断重复,我希望它在用户被发送到另一个视图控制器后停止。

func check(){
    let current = PFUser.currentUser()?.objectForKey("username")
    let check = PFQuery(className: "Requests")
    check.whereKey("username", equalTo: current!)
    check.whereKey("requestResponded", equalTo: "True")
    check.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
        if error != nil || objects == nil{
            print("Request rejected.")
        } else {
            for object in objects!{
                let service = object["service"] as! NSValue
                print(service)
                if service == 1{
                    self.performSegueWithIdentifier("detailedRequest", sender: self)
                    print("detailedRequest")
                } else {
                    self.performSegueWithIdentifier("normalRequest", sender: self)
                    print("normal")
                }
            }
            print("Successfully retrieved that object.")
        }
    })
}

self.timer = NSTimer.scheduledTimerWithTimeInterval(10.0, target: self, selector: #selector(self.check), userInfo: nil, repeats: true)

1 个答案:

答案 0 :(得分:0)

声明viewDidDisappear功能并在其中使计时器无效。每次viewController消失时,都会自动调用此函数。

这样的事情:

override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(true)
    self.timer.invalidate()
}