How do I create a NSTimer on a background thread?

前端 未结 10 650
感情败类
感情败类 2020-11-28 02:04

I have a task that needs to be performed every 1 second. Currently I have an NSTimer firing repeatedly every 1 sec. How do I have the timer fire in a background thread (no

10条回答
  •  庸人自扰
    2020-11-28 02:49

    Swift only (although can probably be modified to use with Objective-C)

    Check out DispatchTimer from https://github.com/arkdan/ARKExtensions, which "Executes a closure on specified dispatch queue, with specified time intervals, for specified number of times (optionally). "

    let queue = DispatchQueue(label: "ArbitraryQueue")
    let timer = DispatchTimer(timeInterval: 1, queue: queue) { timer in
        // body to execute until cancelled by timer.cancel()
    }
    

提交回复
热议问题