Discussion 議論
By default, NSOperation
doesn’t report progress until total
is set. When total
is set, the queue begins reporting progress. Each operation in the queue contributes one unit of completion to the overall progress of the queue for operations that are finished by the end of main
. Operations that override start
and don’t invoke super don’t contribute to the queue’s progress.
Warning 警告
Be careful to avoid race conditions and backward progress when updating total
. Consider a queue with a progress that has a completed
equal to 5
and a total
equal to 10
, representing 50% completion. If you add 90 more operations to the queue, the total
is now 100
, and the progress completion reports 5%. If the progress object is connected to a progress bar, the bar would visibly jump backward from 50% to 5%, which may not be desirable.
To update total
in a thread-safe manner, use the add
method. This method ensures that the operation queue completes the operations in the queue, preventing an inadvertent backward jump in progress.