AP.
AP.

Reputation: 5323

NSOperation : how to pause and resume

I have an NSOperation used to copy files. I copy files using write() and then have the ability between each write() to "pause" the copy. However I do not know how to:

Thanks for your help

Upvotes: 3

Views: 1668

Answers (2)

Motti Shneor
Motti Shneor

Reputation: 2194

I'll recap on the beautiful comment, with a little "philosophy" in advance.

Use of NSOperation queues came first and foremost to RID US from the need to semaphore, to trigger, to watch for triggers, to poll on conditions and all that.

If you find yourself meddling with these "old style" concurrency issues, you are most likely on the wrong trail, off the NSOperationQueue road. It MUST be simple. it MUST be without "threading edges".

The important point is: you do NOT pause an NSOperation. You pause an NSOperationQueue. When paused, the queue will let executing operations finish - but will not dequeue more operations, until the queue is resumed.

This implies breaking your long NSOperation "that copies files using write()" into several "smaller" NSOperations. Maybe one per-file, or, if you need to pause the copying of a single-file in the middle - have each NSOperation responsible for a specific write() call.

To order and synchronize these smaller NSOperations you can put to use the huge arsenal of tools packed with NSOperationQueue and NSOperation.

  • You can use dependencies to order of execution of the chain. and to maintain several concurrent chains.
  • You can use cancellation to break a chain and stop it.
  • You can simply pause and resume the NSOperationQueue to achieve your effect, knowing that each NSOperation of yours (say copying 100KB of file data) is handled correctly.
  • You can get your "progress" for free using NSProgress that is so nicely integrated with NSOperationQueue.
  • You can offload "dependent" operations to other queues of higher or lower priorities, to optimize your performance (say one for the higher-level UI handling, and one for the lower-level file-system manipulations.

It is really endless and very easy to do.

Last - if you want to enjoy "better dependency" mechanism than what NSOperation gives out of the box - look at my own question here and read both the beautiful answers by others, and my own take of the problem there.

Upvotes: 0

CRD
CRD

Reputation: 53000

You can do this with a semaphore; but rather than using the semaphore to control access to a section of code (i.e. acquire the lock, use the resource, release the lock model) you instead just think of it as a gate - acquire, test condition/block, release and continue.

NSCondition will give you a high-level semaphore. In the overview there step 3 is "test if I should pause" and steps 4 & 5 become "no, so do nothing".

Upvotes: 2

Related Questions