如何在后台线程上每秒执行一个方法,这样它就不会影响应用程序的性能

How to execute a method every second on a background thread so it doesn't affect the performance of the app

我试图每 30 秒访问一次我的数据库,但是,每当该方法执行时,我都可以清楚地看到应用程序的性能下降。

到目前为止,这是我当前的代码:

var timer = Timer()

override func viewDidLoad() {
    super.viewDidLoad()
    scheduledTimerWithTimeInterval()

}

func scheduledTimerWithTimeInterval(){
    timer = Timer.scheduledTimer(timeInterval: 30, target: self, selector: #selector(self.updateCounting), userInfo: nil, repeats: true)
}

@objc func updateCounting(){
    getDatabaseInfo()
}

我正在尝试做同样的事情,只是我想在后台线程上执行 getDatabaseInfo() 方法,这样应用程序的性能就不会受到影响。

使用 Grand Central Dispatch :

DispatchQueue.global(qos: .background).async {
    getDatabaseInfo()
}

您可以尝试使用以下代码。

var timer = Timer()

override func viewDidLoad() {
    super.viewDidLoad()
    scheduledTimerWithTimeInterval()

}

func scheduledTimerWithTimeInterval(){
    timer = Timer.scheduledTimer(timeInterval: 30, target: self, selector: #selector(self.updateCounting), userInfo: nil, repeats: true)
}

@objc func updateCounting(){

    DispatchQueue.global(qos: .background).async {
        print("This is run on the background queue")
        getDatabaseInfo()
        DispatchQueue.main.async {
            print("This is run on the main queue, after the previous code in outer block")
        }
    }
}

您可以 运行 直接在后台队列中使用 DispatchSourceTimer:

计时器
private var timer: DispatchSourceTimer?

func startTimer() {
    let queue = DispatchQueue(label: Bundle.main.bundleIdentifier! + ".timer")
    timer = DispatchSource.makeTimerSource(queue: queue)
    timer!.schedule(deadline: .now(), repeating: .seconds(1))
    timer!.setEventHandler { [weak self] in
        // do whatever stuff you want on the background queue here here

        getDatabaseInfo()

        DispatchQueue.main.async {
            // update your model objects and/or UI here
        }
    }
    timer!.resume()
}

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

使用Timer,你将它安排在主队列的运行循环上,然后让它将任务分派到后台队列,然后将UI/model更新分派回主队列队列。像上面一样使用调度计时器绕过第一步,运行直接在 GCD 后台队列上设置计时器。