在辅助线程上调度 NSStreams
在我正在开发的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我也喜欢 detachNewThreadSelector... 方法,但仅供参考,您可以使用 NSOperation 和 NSOperationQueue。它将把非并发操作扔到单独的线程上。
要让流继续运行,您需要查看此类内容:
一定要查看 Apple 示例“PictureSharing”,网址为 http://developer.apple.com/library/mac/#samplecode/PictureSharing。
特别是,复制
FileSendOperation
和FileReceiveOperation
类以及QRunLoopOperation
。我还使用LinkedImageFetcher
示例的QWatchedOperationQueue
类,它与 PictureSharing 类配合良好。我采用了它们的 *SendOperation 和 *ReceiveOperation 类,并将它们转换为发送/接收我需要的内容的类(一些 NSData)。然后就很简单了:
I like the
detachNewThreadSelector
... approach too, but FYI you can useNSOperation
andNSOperationQueue
. It'll throw non-concurrent operations onto separate threads.To get the streams going, you're looking at this kind of thing:
Definitely look at the Apple sample "PictureSharing" at http://developer.apple.com/library/mac/#samplecode/PictureSharing.
In particular, copy the
FileSendOperation
andFileReceiveOperation
classes, and theQRunLoopOperation
. I also use theLinkedImageFetcher
sample'sQWatchedOperationQueue
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:
我只是做了一些谷歌搜索,然后我想出了这个:
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.