Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pause NSOperation

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.

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.

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?

like image 866
kpower Avatar asked Sep 08 '10 11:09

kpower


People also ask

How do you pause NSOperation?

add a isPaused flag to your NSOperation subclass. implement a copy/move method for the operation's data. if paused, setCancelled: (watch for this change in -main ) create a new operation moving the state from paused operation to new operation.

How do you pause an operation in Swift?

suspend() when you send the operation, and have the code that gets the response call . resume() . Then wherever you want to wait for the response before continuing, just put a dummy queue. sync({ print("done waiting")}) and it will automatically wait until .

What is NSOperation and Nsoperationqueue in IOS?

Overview. An operation queue invokes its queued NSOperation objects based on their priority and readiness. After you add an operation to a queue, it remains in the queue until the operation finishes its task. You can't directly remove an operation from a queue after you add it. Note.


1 Answers

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". Within the operation itself, observe that property and handle accordingly if it ever changes.

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.

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.

like image 146
Jackie Treehorn Avatar answered Sep 21 '22 01:09

Jackie Treehorn