ios 在 Swift 中每 x 分钟做一些事情

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25951980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:39:57  来源:igfitidea点击:

Do something every x minutes in Swift

iosswiftsetintervalintervalsrepeat

提问by JOSEFtw

How can I run a function every minute? In JavaScript I can do something like setInterval, does something similar exist in Swift?

如何每分钟运行一个函数?在 JavaScript 中,我可以做类似的setInterval事情,Swift 中是否存在类似的东西?

Wanted output:

想要的输出:

Hello World once a minute...

你好世界每分钟一次...

回答by Unheilig

var helloWorldTimer = NSTimer.scheduledTimerWithTimeInterval(60.0, target: self, selector: Selector("sayHello"), userInfo: nil, repeats: true)

func sayHello() 
{
    NSLog("hello World")
}

Remember to import Foundation.

记得导入Foundation。

Swift 4:

斯威夫特 4:

 var helloWorldTimer = Timer.scheduledTimer(timeInterval: 60.0, target: self, selector: #selector(ViewController.sayHello), userInfo: nil, repeats: true)

 @objc func sayHello() 
 {
     NSLog("hello World")
 }

回答by Rob

If targeting iOS version 10 and greater, you can use the block-based rendition of Timer, which simplifies the potential strong reference cycles, e.g.:

如果针对 iOS 10 及更高版本,您可以使用 的基于块的再现Timer,这简化了潜在的强引用循环,例如:

weak var timer: Timer?

func startTimer() {
    timer?.invalidate()   // just in case you had existing `Timer`, `invalidate` it before we lose our reference to it
    timer = Timer.scheduledTimer(withTimeInterval: 60.0, repeats: true) { [weak self] _ in
        // do something here
    }
}

func stopTimer() {
    timer?.invalidate()
}

// if appropriate, make sure to stop your timer in `deinit`

deinit {
    stopTimer()
}


While Timeris generally best, for the sake of completeness, I should note that you can also use dispatch timer, which is useful for scheduling timers on background threads. With dispatch timers, since they're block-based, it avoids some of the strong reference cycle challenges with the old target/selectorpattern of Timer, as long as you use weakreferences.

虽然Timer通常是最好的,但为了完整起见,我应该注意您也可以使用调度计时器,这对于在后台线程上调度计时器很有用。使用调度计时器,因为它们是基于块的,所以只要您使用引用,它就可以避免旧的target/selector模式的一些强引用循环挑战。Timerweak

So:

所以:

var timer: DispatchSourceTimer?

func startTimer() {
    let queue = DispatchQueue(label: "com.domain.app.timer")  // you can also use `DispatchQueue.main`, if you want
    timer = DispatchSource.makeTimerSource(queue: queue)
    timer!.schedule(deadline: .now(), repeating: .seconds(60))
    timer!.setEventHandler { [weak self] in
        // do whatever you want here
    }
    timer!.resume()
}

func stopTimer() {
    timer?.cancel()
    timer = nil
}

deinit {
    self.stopTimer()
}

For more information, see the the Creating a Timersection of Dispatch Source Examplesin the Dispatch Sourcessection of the Concurrency Programming Guide.

有关更多信息,请参阅并发编程指南调度源部分中的调度源示例创建计时器部分



For Swift 2, see previous revision of this answer.

对于 Swift 2,请参阅此答案的先前修订版

回答by John T

Here's an update to the NSTimeranswer, for Swift 3 (in which NSTimerwas renamed to Timer) using a closure rather than a named function:

这是NSTimer答案的更新,对于使用闭包而不是命名函数的Swift 3(其中NSTimer已重命名为Timer):

var timer = Timer.scheduledTimer(withTimeInterval: 60, repeats: true) {
    (_) in
    print("Hello world")
}

回答by algrid

If you can allow for some time drifthere's a simple solution executing some code every minute:

如果您可以允许一些时间漂移,这里有一个简单的解决方案,每分钟执行一些代码:

private func executeRepeatedly() {
    // put your code here

    DispatchQueue.main.asyncAfter(deadline: .now() + 60.0) { [weak self] in
        self?.executeRepeatedly()
    }
}

Just run executeRepeatedly()once and it'll be executed every minute. The execution stops when the owning object (self) is released. You also can use a flag to indicate that the execution must stop.

只需运行executeRepeatedly()一次,它将每分钟执行一次。当拥有对象 ( self) 被释放时,执行停止。您还可以使用标志来指示必须停止执行。

回答by Bas

You can use Timer(swift 3)

您可以使用Timer(swift 3)

var timer = Timer.scheduledTimerWithTimeInterval(60, target: self, selector: Selector("function"), userInfo: nil, repeats: true)

In selector() you put in your function name

在 selector() 中输入函数名称

回答by Can

In swift 3.0 the GCD got refactored:

在 swift 3.0 中,GCD 被重构了:

let timer : DispatchSourceTimer = DispatchSource.makeTimerSource(flags: [], queue: DispatchQueue.main)

timer.scheduleRepeating(deadline: .now(), interval: .seconds(60))
timer.setEventHandler
{
    NSLog("Hello World")
}
timer.resume()

This is specially useful for when you need to dispatch on a particular Queue. Also, if you're planning on using this for user interface updating, I suggest looking into CADisplayLinkas it's synchronized with the GPU refresh rate.

当您需要在特定队列上调度时,这特别有用。此外,如果您打算将其用于用户界面更新,我建议您查看CADisplayLink它是否与 GPU 刷新率同步。