且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在后台线程上执行工作后写入NSOutputStream不起作用

更新时间:2023-02-09 18:40:38

万一有人偶然发现了这个东西,并且碰巧看到了我所看到的同一件事(并且认为同一件事就是问题):

In case anyone ever stumbles up on this and just happens to see the same thing I saw (and think the same thing is the problem):

问题不在于线程是否引起了问题-只是写这篇文章使我意识到自己是多么荒谬.问题似乎出在等待很长时间以接收数据之后,底层的TCP套接字超时了.我通过每秒发送一次心跳消息来解决该问题,该消息使套接字保持活动状态,直到我准备好开始实际发送数据为止.

The problem wasn't that the threading was causing issues - just writing that post made me realize how absurd I was being. The problem appears to have been that the underlying TCP sockets were timing out after waiting for so long to receive data. I solved the problem by sending out a heartbeat message once a second, which keeps the sockets alive until I'm ready to actually start sending data.