如何使 NSOperation 并发而不添加到 iPhone 中的 NSOperationQueue
我正在尝试通过从 NSOperation 子类化来创建 NSOperation。我希望我的操作在单独的线程上执行,并且它应该支持取消选项,即我应该能够在任何时间点停止线程(或操作) 。我尝试将我的操作实例添加到 NSOperationQueue 中,一切正常,但“操作”在一段时间后执行,这使我的应用程序变慢。所以我尝试通过调用 [theOperation start]; 单独运行我的操作代码>其相当快,但在主线程上执行。如何使 NSOperation 在单独的线程上运行并具有取消选项,请帮忙。
I am trying to create an NSOperation by subclassing from NSOperation.I want my operation to be executed on a separate thread as well as it should support canceling option i.e i should be able to stop the thread(or the operation) at any point of time.I tried adding my operation instance to NSOperationQueue,everything is working fine but "the operation" is being executed after some time which makes my application slow.So i tried running my operation alone by calling [theOperation start];
its pretty fast but executing on the main thread.How to make the NSOperation run on separate thread with canceling option please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要么:
1) 不要将其设为 NSOperation 并显式创建线程。
2) 使用第二个操作队列和/或提高优先级。
3) 对结果使用容器类型。当您立即需要时,取消后台加载操作并手动加载。
either:
1) do not make it an NSOperation and explicitly create a thread.
2) use a second operation queue and/or increase the priority.
3) use a container type for the result. when you need it immediately, cancel the background load operation and load manually.
我相信您需要实现
- (BOOL)isConcurrent
:但有一些影响。阅读有关 NSOperation 的 Apple 参考文档,特别是子类化注释部分。另请注意,如果您希望操作可取消,您需要自己实现取消行为:例如,在您的
main
选择器中,您需要检查操作是否已被取消,如果取消则立即返回有。I believe you need to implement
- (BOOL)isConcurrent
:But there are implications. Read the Apple reference docs on NSOperation, particularly the section Subclassing Notes. Also note that if you want the operation to be cancellable, you need to implement the cancelling behaviour yourself: eg, in your
main
selector you need to check if the operation has been cancelled, and immediately return if it has.