3

I've written a quick class for 'piping' the contents of an InputStream to multiple OutputStreams. It starts this by doing:

self.from.schedule(in: RunLoop.current, forMode: RunLoopMode.defaultRunLoopMode)
self.to.forEach { to in
    to.schedule(in: RunLoop.current, forMode: RunLoopMode.defaultRunLoopMode)
}

This works fine when I'm in the main thread, but if I try to do it on a background thread nothing ever fires. From my research I understand that this is because the RunLoop only automatically runs on the main thread.

It's often a download task, so from my understanding I don't really want that on the main thread. So how can I best have this run on a background queue?

Alastair
  • 5,894
  • 7
  • 34
  • 61
  • How are you starting this background thread? Does it have a run loop? MCVE! – Ssswift Oct 03 '17 at 16:08
  • Do you *run* the runloop on the background queue? Compare https://stackoverflow.com/questions/9819872/how-to-make-nsrunloop-work-inside-a-separate-thread. – Martin R Oct 03 '17 at 16:08
  • At the moment I don't have a background queue - I'd like to know the best way to create one. I've used DispatchQueue.global() before, but I'm not at all clear on how that maps to a RunLoop, and how I might then go about running that RunLoop. – Alastair Oct 03 '17 at 16:15
  • @Alastair Do you find solution for this ? – Varun Naharia Jan 03 '18 at 13:03

0 Answers0