我正在编写一个点对点蓝牙聊天应用程序。我目前正在做的是:
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()
我目前面临self.channel
的CBL2CAPChannel
问题是它为每对通道生成新线程,最终有太多线程挂起。
在这种情况下设置CBL2CAPChannel
s 的正确方法是什么?Apple 的文档为此使用了主线程,这是出乎意料的,当有很多连接时可能会导致问题。