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

帅比萌擦擦* 提交于 2020-02-05 04:25:09

问题


I am trying to access my database every 30 seconds, however, whenever the method executes I can clearly see a performance dip in the application.

So far this is my current code:

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()
}

I am trying to do the same thing, except I would like to execute the getDatabaseInfo() method on a background thread so that the app's performance is not compromised.


回答1:


Use Grand Central Dispatch :

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



回答2:


You can try with below code.

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")
        }
    }
}



回答3:


You can just run the timer directly on the background queue by using a 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
}

Using Timer, you schedule it on the main queue's run loop and then have it dispatch the task to a background queue, and then dispatch UI/model updates back to the main queue. Using dispatch timer, like above, bypasses that first step, running the timer directly on a GCD background queue.



来源:https://stackoverflow.com/questions/53928105/how-to-execute-a-method-every-second-on-a-background-thread-so-it-doesnt-affect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!