rapidTimer in swift 3

时间:2016-10-28 16:11:05

标签: timer swift3

这是在swift 3中启动重复计时器的正确方法吗?

  • 请注意,此代码也必须在iOS 8上运行。
  • userInfo是否需要是字典?
  • 这段代码可以改进吗?

```

class ABContactManager : NSObject {
    func waitForContactsToLoad(_ onSuccess: @escaping (() -> ())) {
        Timer.scheduledTimer(
           timeInterval: 0.5,
           target: self,
           selector: #selector(ABContactManager.timerFired(timer:)),
           userInfo: ["callback" : onSuccess],
           repeats: true)
    }
    func timerFired(timer: Timer) {
        logInfo("keep polling contacts")
        if let userInfo = timer.userInfo as? [String : Any] {
            if let onSuccess = userInfo["callback"] as? (() -> ()) {
                if self.loadingStatus == .loaded {
                    logInfo("stop polling -- loaded contacts issue callback")
                    onSuccess()
                    timer.invalidate()
                }
            }
        }
    }
}

```

0 个答案:

没有答案