暂停NSOperation [英] Pause NSOperation

查看:143
本文介绍了暂停NSOperation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 NSOperationQueue ,其中有一些 NSOperations NSInvocationOperations ,特别是)。此操作会进行一些计算并相应地更改UI元素的状态(当然,通过 performSelectorOnMainThread:... ),通常使用动画。

I have NSOperationQueue with some NSOperations in it (NSInvocationOperations, in particular). This operations do some calculations and change states of UI elements accordingly (of course, via performSelectorOnMainThread:...), often with use of animations.

我的UI有UINavigationViewController和一些用于导航到另一个视图的按钮。因此,用户可以保留当前视图,而计算/动画仍在进行中。我需要的是以某种方式停止此操作直到用户返回到当前视图。

My UI has UINavigationViewController and some buttons for navigation to another views. So user can leave current view, while calculations / animations are still in progress. And what I need is to stop this somehow until user comes back to current view.

我找到的唯一解决方案是创建一些线程安全的布尔标志 - 并检查它在所有线程中(类似于:while!flag sleep_for_some_time;)。还有更好的东西吗?

The only solution I found is to create some thread-safe boolean flag - and to check it in all threads (something like: while !flag sleep_for_some_time;). Is there something better?

推荐答案

问题有点模糊,所以很难说不知道所有代码都在游戏中。话虽如此,我可以通过以下方式解决问题:

The question is a bit vague, so it's hard to say without knowing all of the code in play. With that said, I may approach the problem by:

选项1.在您的NSOperation的子类中,添加您自己的原子KVO属性isPaused。在操作本身内,观察该属性并在其发生变化时进行相应处理。

Option 1. In your subclass of NSOperation, add your own atomic KVO property "isPaused". Within the operation itself, observe that property and handle accordingly if it ever changes.

选项2.您是否曾暂停操作队列本身?如果是这样,请考虑从您的操作中观察该属性,并且如果该值发生更改,则每个属性都可以独立执行。

Option 2. Are you ever suspending the Operation Queue itself? If so, consider observing that property from within your operations, and each one independently can take action if that value changes.

选项3.取消队列中的所有操作,以及如果视图再次出现,只需重新启动新操作。

Option 3. Cancel all operations in the queue, and if the view appears again, just restart with new operations.

但总的来说,暂停正在进行的操作没有灵丹妙药。你必须烘焙自己的解决方案。但伤害不应该太糟糕。

Overall, though, there is no magic bullet for pausing operations already in progress. You'll have to bake your own solution. The damage shouldn't be too bad though.

这篇关于暂停NSOperation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆