CADisplayLink in a DispatchQueue

802 Views Asked by At

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

1

There are 1 best solutions below

1
bochiu On

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