在辅助线程上调度 NSStreams

发布于 2024-09-25 08:30:56 字数 351 浏览 5 评论 0原文

在我正在开发的 iPad 应用程序中,我需要将网络处理放在单独的线程上,因为它偶尔会阻塞应用程序的 UI。目前,我已经创建了一个 Connection 对象,其中包含所有网络逻辑(NSStreams 及其 delegate 方法)。

主要障碍是如何创建辅助线程并在该线程的运行循环上调度 NSStreams。我是否显式创建一个由 Connection 对象拥有的 NSThread

我一直在尝试 NSOperation,但这似乎不是最好的解决方案,因为我觉得需要一个专门处理网络事件的线程。

欢迎指点和建议。任何示例代码也可能会有帮助。

巴特

In an iPad app I'm developing, I need to put the network handling on a separate thread since it occasionally blocks the UI of the app. At the moment, I have created a Connection object in which all the networking logic goes (NSStreams and its delegate methods).

The main obstacle is how to create the secondary thread and schedule the NSStreams on the run loop of this thread. Do I explicitly create an NSThread that is then owned by the Connection object?

I have been experimenting with NSOperation, but this didn't seem like the best solution as I feel the need for a thread dedicated to handling networking events.

Pointers and advice are welcome. Any sample code might be helpful as well.

Bart

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷︶言冷语的世界 2024-10-02 08:30:56

我也喜欢 detachNewThreadSelector... 方法,但仅供参考,您可以使用 NSOperation 和 NSOperationQueue。它将把非并发操作扔到单独的线程上。

要让流继续运行,您需要查看此类内容:

[self.outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:mode];

一定要查看 Apple 示例“PictureSharing”,网址为 http://developer.apple.com/library/mac/#samplecode/PictureSharing

特别是,复制 FileSendOperationFileReceiveOperation 类以及 QRunLoopOperation。我还使用 LinkedImageFetcher 示例的 QWatchedOperationQueue 类,它与 PictureSharing 类配合良好。我采用了它们的 *SendOperation 和 *ReceiveOperation 类,并将它们转换为发送/接收我需要的内容的类(一些 NSData)。

然后就很简单了:

 FileSendOperation *op;
 op = [[[FileSendOperation alloc] initWithFilePath:somePath outputStream:outStream ] autorelease];

 [self.queue addOperation:op finishedAction:@selector(networkingDone:)];

I like the detachNewThreadSelector... approach too, but FYI you can use NSOperation and NSOperationQueue. It'll throw non-concurrent operations onto separate threads.

To get the streams going, you're looking at this kind of thing:

[self.outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:mode];

Definitely look at the Apple sample "PictureSharing" at http://developer.apple.com/library/mac/#samplecode/PictureSharing.

In particular, copy the FileSendOperation and FileReceiveOperation classes, and the QRunLoopOperation. I also use the LinkedImageFetcher sample's QWatchedOperationQueue class, which works well with the PictureSharing classes. I took their *SendOperation and *ReceiveOperation classes and turned them into classes sending/receiving what I needed (some NSData).

Then it's as easy as:

 FileSendOperation *op;
 op = [[[FileSendOperation alloc] initWithFilePath:somePath outputStream:outStream ] autorelease];

 [self.queue addOperation:op finishedAction:@selector(networkingDone:)];
陌路终见情 2024-10-02 08:30:56

我只是做了一些谷歌搜索,然后我想出了这个:

http ://kdl.nobugware.com/post/2008/12/22/nsthread-iphone-template/

我认为这就是您所需要的;)

编辑:
http://www.xprogress.com/post-36-threading-tutorial-using-nsthread-in-iphone-sdk-objective-c/" xprogress.com/post-36-threading-tutorial-using-nsthread-in-iphone-sdk-objective-c/
也许这对......

如果您阅读代码,您会看到performSelectorOnMainThread(或其他东西),这样您就可以在线程之间发送回信息。

I did just some googling, and I came up with this:

http://kdl.nobugware.com/post/2008/12/22/nsthread-iphone-template/

I think this is what you need ;)

EDIT:
http://www.xprogress.com/post-36-threading-tutorial-using-nsthread-in-iphone-sdk-objective-c/
Maybe that is usefull to...

If you read the code, you see performSelectorOnMainThread (or something) so you can send back info from thread to thread.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文