Reputation: 5323
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
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" NSOperation
s. 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 NSOperation
s you can put to use the huge arsenal of tools packed with NSOperationQueue and NSOperation.
NSOperationQueue
to achieve your effect, knowing that each NSOperation of yours (say copying 100KB of file data) is handled correctly.NSProgress
that is so nicely integrated with NSOperationQueue
.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
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