Understanding NSBlockOperation

1.2k views Asked by At

I'm getting into NSBlockOperation and I have some questions. Notably, the documentation for addExecutionBlock says:

Discussion

The specified block should not make any assumptions about its execution environment.

Calling this method while the receiver is executing or has already finished causes an NSInvalidArgumentException exception to be thrown.

What kind of situation will throw NSInvalidArgumentException? What really doesn "while receiver is executing" mean? What can cause this?

2

There are 2 answers

10
quellish On BEST ANSWER

You can't use addExecutionBlock: to add an execution block while the operation is running or has already completed. That's all it means.

0
Ken Thomases On

A block operation object can have zero or more execution blocks associated with it. When the block operation is started, all of its associated execution blocks are submitted for concurrent execution. The warning is that you can't add more execution blocks to the operation after this point.

You can create more block operation objects and add execution blocks to those. Each block operation is started separately from others, so the rule about adding more execution blocks is evaluated separately.

Typically, you would create a block operation, add whatever execution blocks to it that you want, and then queue the operation onto an operation queue. Once the operation has been queued, it might start at any time (subject to readiness, which is subject to dependencies). So, it's best to not attempt to add execution blocks once it's been queued.