跨视图控制器共享 NSOperationQueue?
我使用 NSOperationQueue 来管理 HTTP 连接(使用 ASI-HTTPRequest)。由于我有多个视图并且需要让这些不同的视图请求 HTTP 连接,因此我应该尝试在应用程序委托中创建一个全局 NSOperationQueue,还是应该在每个视图中创建一个全局 NSOperationQueue?我对 NSOperationQueue 不熟悉。
我想知道a)最佳实践是什么,b)如果没有最佳实践,需要权衡什么(如果有的话)。
我确实尝试将操作队列放在处理服务器连接的类中(作为属性),但任务从未触发。无法弄清楚,但 [队列操作] = 0。如果有人知道这个问题的解决方案,我认为这将是放置它的最佳位置。
I'm using an NSOperationQueue to manage HTTP connections (using ASI-HTTPRequest). Since I have multiple views and the need to have these different views requesting HTTP connections, should I try to create a global NSOperationQueue in the app delegate, or should I have one in each of the views? I'm not familiar with NSOperationQueue.
I'd like to know a) what the best practice is and b) if there is no best practice, what the tradeoffs are if any.
I did try to put the operation queue in the class (as a property) where I handle the server connections but the task never fired. Couldnt figure it out but [queue operations] = 0. If someone knows a solution to this, I presume this would be the best place to put it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我通过在 NSOperationQueue 上添加一个类方法解决了这个问题,我认为苹果已经错过了这个方法;共享操作队列。我将其添加为 NSOperationQueue 上的一个类别,如下所示:
这样,除非我确实需要,否则我不需要管理一大堆队列。我可以从所有视图控制器轻松访问共享队列。
我什至向 NSObject 添加了一个类别,以便更轻松地在此共享队列上添加新操作:
I have solved this by adding a class method on NSOperationQueue that I think Apple has missed; a shared operation queue. I add this as a category on NSOperationQueue as this:
This way I do not need to manage a whole bunch of queues unless I really need to. I have easy access to a shared queue from all my view controllers.
I have even added a category to NSObject to make it even easier to add new operations on this shared queue:
我个人的偏好是有一个单例来管理所有 http 请求。然后,每个视图都会要求单例进行 http 调用,将自己作为该调用的委托传递,然后单例将委托交给 NSOperation 并调用,然后在调用完成后 NSOperation 回调。
My personal preference for this is to have a singleton that manages all http requests. Each view would then ask the singleton to make the http call, passing itself as a delegate for that call then the singleton hands that delegate and call off to an NSOperation and then NSOperation calls back once the call is done.
如果您已经有一个指向处理每个视图/视图控制器中的连接的类的指针,那么您没有理由还需要有一个指向操作队列的指针。
我想你想做的是这样的:a)视图(控制器)将url(+数据)传递给服务器处理对象,b)服务器处理对象创建操作并将其放入队列中,只有它有一个指向的指针。
如果您不提供更多详细信息,很难弄清楚为什么这不起作用。
我强烈建议 看看 ASIHTTPRequest 它提供了一个 NetworkQueue 类处理此类任务。它有几个方便的委托字段,可让您注册以跟踪进度、了解下载或上传何时完成等。
If you already have a pointer to a class that handles connections in each view/view controller, there's no reason you would also need to have a pointer to the operation queue.
I suppose what you want to do is something like: a) view(Controller) hands url(+data) to server handling object, b) server handling objects creates operation and puts it in a queue that it and only it has a pointer to.
It's hard to figure out why that didn't work if you don't provide more detail.
I highly recommend taking a look at ASIHTTPRequest which provides a NetworkQueue class to handle this kind of task. It has several convenient delegate fields that lets you register to keep track of progress, know when a download or upload finished etc.