ios - 未为使用 getBoundStreams 创建的 InputStream 调用 StreamDelegate 方法
问题描述
我正在创建一对绑定的流并为我的输入流设置一个委托,如下所示:
class ViewController: UIViewController {
private var inputStream: InputStream?
override func viewDidLoad() {
super.viewDidLoad()
inputStream = createStream()
inputStream?.delegate = self
// Open inputStream an try to read from it...
}
func createStream() -> InputStream {
var output: OutputStream? = nil
var input: InputStream? = nil
Stream.getBoundStreams(withBufferSize: 1024, inputStream: &input, outputStream: &output)
DispatchQueue.global().async {
output!.open()
for _ in 0..<3 {
output!.write(data: "str".data(using: .utf8)!)
sleep(1)
}
output!.close()
}
return input!
}
}
extension ViewController: StreamDelegate {
func stream(_ aStream: Stream, handle eventCode: Stream.Event) {
print(eventCode)
}
}
extension OutputStream {
@discardableResult
func write(data: Data) -> Int {
return data.withUnsafeBytes {
write($0.bindMemory(to: UInt8.self).baseAddress!, maxLength: data.count)
}
}
}
Bt myStreamDelegate
永远不会被调用。有没有办法来解决这个问题?
解决方案
- 你错过了一个
open()
电话InputStream
- 两个流都必须附加到 runloop 以取得进展,例如
stream?.schedule(in: RunLoop.current, forMode: RunLoop.Mode.default)
推荐阅读
- mongodb - 在 MongoDB 中组合组 - 聚合
- java - 如何在我的 Java 代码中进行更流畅的移动
- nearprotocol - Near 协议 FunctionCallError(MethodResolveError(MethodNotFound))
- angular - 如何将子组件的增量传递给父组件中的计数器?
- python - Django 表单 - 需要一个显示一组问题的表单,每个问题都有一个可能的答案列表
- ansible - 如何在 ansible include_role 选项中传递文件/文件夹
- dom - addEventListener('DOMContentLoaded'),function(){ - 需要 2 个参数,但只有 1 个存在
- python - Pandas pd.pivot_table 其中 aggfunc 返回子组中唯一项目的集合或列表
- javascript - 如何在变量中返回对象 xml - Javascript?
- java - 带有视频呼叫摄像头权限的 Android WebView Vonage API 被拒绝错误