I'm writing a little peer-to-peer bluetooth chat app. What I'm currently doing is:
let thread = Thread(block: { [weak self] in
guard let `self` = self else { return }
self.channel.inputStream.delegate = self
self.channel.inputStream.schedule(in: .current, forMode: .defaultRunLoopMode)
self.channel.inputStream.open()
self.channel.outputStream.delegate = self
self.channel.outputStream.schedule(in: .current, forMode: .defaultRunLoopMode)
self.channel.outputStream.open()
RunLoop.current.run()
})
thread.start()
Where self.channel
is CBL2CAPChannel
The problem I'm currently facing is that it generates new thread for each pair of channels and eventually there are too many threads hanging around.
What is proper way to set up CBL2CAPChannel
s in this case? Apple's docs are using main thread for this, which is unexpected and could lead to problems when there are a lot of connections.