简体   繁体   中英

CADisplayLink in a DispatchQueue

I am trying to run a display link in a thread other than main but it simply doesn't work. I have a simple dispatch queue created like queue = DispatchQueue(label: "xyz") and then I create the display link as usual:

queue.async {
  self.displayLink = CADisplayLink(target: self, selector: #selector(render))
  self.displayLink.add(to: .current, forMode: .common)
}

The selector never gets called. Upon checking the currentMode of the RunLoop I see it is nil. What am I missing?

Thanks

Due to the reason that your queue is non-main, the current run loop won't trigger by itself.

You should call current.run() manually after displayLink been added.

queue.async {
  self.displayLink = CADisplayLink(target: self, selector: #selector(render))
  let current = RunLoop.current
  self.displayLink.add(to: current, forMode: .common)
  current.run()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM