今天我正在测试我的项目,我在两个不同的 Controller (一个 ViewController 和一个 TCPController)中构建了该项目。我的 ViewController 实例化了更新输出和输入流的 TCPController(单例)。现在,在测试中,我可以确定 GUI 界面上存在一些滞后,这很容易归咎于 TCPController。
在我使用苹果网站上的标准教程之前,是否有关于如何线程化 TCP Controller (客户端)的最佳实践:https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Multithreading/CreatingThreads/CreatingThreads.html
非常欢迎提供有关如何解决此问题的示例。
最佳答案
阅读文档总是一个好主意。
您所做的事情部分取决于您的沟通框架。大多数好的框架已经提供了异步方法。如果你的没有。寻找其他东西。
除此之外,通常您会希望在后台线程中执行代码。如果这是一项很长的工作,那么以下应该可以解决问题......
dispatch_queue_t commQ = dispatch_queue_create("some.unique.labe", 0);
dispatch_async(commQ, ^{
// Now, any code running in this block is running in a different thread.
// When you get done, and want to talk to the UI, you must use the main
// queue for any UIKit calls...
dispatch_async(dispatch_get_main_queue(), ^{
// Now this code is running on the main queue
// Do all your UI stuff here...
});
});
dispatch_release(commQ);
关于objective-c - 在 iOS 中线程化 TCP Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10837311/