简体   繁体   English

暂停NSOperation

[英]Pause NSOperation

I have NSOperationQueue with some NSOperations in it ( NSInvocationOperations , in particular). 我有NSOperationQueue ,其中有一些NSOperations (特别是NSInvocationOperations )。 This operations do some calculations and change states of UI elements accordingly (of course, via performSelectorOnMainThread:... ), often with use of animations. 此操作会进行一些计算并相应地更改UI元素的状态(当然,通过performSelectorOnMainThread:... ),通常使用动画。

My UI has UINavigationViewController and some buttons for navigation to another views. 我的UI有UINavigationViewController和一些用于导航到另一个视图的按钮。 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. 我需要的是以某种方式停止这一点,直到用户回到当前视图。

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;). 我找到的唯一解决方案是创建一些线程安全的布尔标志 - 并在所有线程中检查它(类似于: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: 话虽如此,我可以通过以下方式解决问题:

Option 1. In your subclass of NSOperation, add your own atomic KVO property "isPaused". 选项1.在NSOperation的子类中,添加自己的原子KVO属性“isPaused”。 Within the operation itself, observe that property and handle accordingly if it ever changes. 在操作本身内,观察该属性并在其发生变化时进行相应处理。

Option 2. Are you ever suspending the Operation Queue itself? 选项2.您是否曾暂停操作队列? If so, consider observing that property from within your operations, and each one independently can take action if that value changes. 如果是这样,请考虑从您的操作中观察该属性,并且如果该值发生更改,则每个属性都可以独立执行。

Option 3. Cancel all operations in the queue, and if the view appears again, just restart with new operations. 选项3.取消队列中的所有操作,如果视图再次出现,则只需使用新操作重新启动。

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. 但是伤害不应该太糟糕。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM