objective-c - 在 iOS 中线程化 TCP Controller

标签 objective-c ios

今天我正在测试我的项目,我在两个不同的 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/

相关文章:

objective-c - NSMutableDictionary 与 CFMutableDictionary

iOS UITableView 隐藏表头空间

ios - 比较两个 NSDictionaries 并找出差异

ios - 在 github 之外创建私有(private) cocoapods 仓库

ios - 嵌入式 AVPlayerControllerView 添加 AVTouchIgnoringView 阻止默认播放器的界面

ios - HTTPS 服务不工作

ios - 如何让两个 iOS 应用共享 plist?

ios - 打开 .xcworkspace 时出错

ios - 如何使用 NSURLConnection 获取呈现的 javascript 源

iphone - 将 myClass 对象分配给 UIView 对象并使用 myClass 选择器(变量)