Open ClassFoundation5.9.0
Progress
Progress
is used to report the amount of work done, and provides a way to allow the user to cancel that work.
class Progress
Since work is often split up into several parts, progress objects can form a tree where children represent part of the overall total work. Each parent may have as many children as required, but each child only has one parent. The top level progress object in this tree is typically the one that you would display to a user. The leaf objects are updated as work completes, and the updates propagate up the tree.
The work that a Progress
does is tracked via a “unit count.” There are two unit count values: total and completed. In its leaf form, a Progress
is created with a total unit count and its completed unit count is updated by setting completedUnitCount
until it matches the totalUnitCount
. The progress is then considered finished.
When progress objects form nodes in trees, they are still created with a total unit count. Portions of the total are then handed out to children as a “pending unit count.” The total amount handed out to children should add up to the parent’s totalUnitCount
. When those children become finished, the pending unit count assigned to that child is added to the parent’s completedUnitCount
. Therefore, when all children are finished, the parent’s completedUnitCount
is equal to its totalUnitCount
and it becomes finished itself.
Children Progress
objects can be added implicitly or by calling addChild(withPendingUnitCount)
on the parent. Implicitly added children are attached to a parent progress between a call to becomeCurrent(withPendingUnitCount)
and a call to resignCurrent
. The implicit child is created with the Progress(totalUnitCount:)
initializer, or by passing the result of Progress.currentProgress
to the Progress(parent:userInfo:)
initializer. Both kinds of children can be attached to the same parent progress object. If you have an idea in advance that some portions of the work will take more or less time than the others, you can use different values of pending unit count for each child.
If you are designing an interface of an object that reports progress, then the recommended approach is to vend a Progress
property and adopt the ProgressReporting
protocol. The progress should be created with the Progress.discreteProgress(withTotalUnitCount:)
class function. You can then either update the progress object directly or set it up to have children of its own. Users of your object can compose your progress into their tree by using the addChild(withPendingUnitCount)
function.
If you want to provide progress reporting for a single method, then the recommended approach is to implicitly attach to a current Progress
by creating an Progress
object at the very beginning of your method using Progress(withTotalUnitCount)
. This progress object will consume the pending unit count, and then you can set up the progress object with children of its own.
The localizedDescription
and localizedAdditionalDescription
properties are meant to be observed as well as set. So are the cancellable
and pausable
properties. totalUnitCount
and completedUnitCount
, on the other hand, are often not the best properties to observe when presenting progress to the user. You should observe fractionCompleted
instead of observing totalUnitCount
and completedUnitCount
and doing your own calculation. Progress
’ default implementation of fractionCompleted
does fairly sophisticated things like taking child Progress
into account.
Superclasses
class NSObject
The root class of most Foundation class hierarchies.
Citizens in Foundation
Conformances
protocol CustomDebugStringConvertible
A type with a customized textual representation suitable for debugging purposes.
protocol CustomStringConvertible
A type with a customized textual representation.
protocol Equatable
A type that can be compared for value equality.
protocol Hashable
A type that can be hashed into a
Hasher
to produce an integer hash value.protocol NSObjectProtocol
The
NSObjectProtocol
groups methods that are fundamental to all Foundation objects.
Members
init(parent: Progress?, userInfo: [ProgressUserInfoKey : Any]?
) Initializes an instance of
Progress
with the specified parent and user info dictionary.convenience init(totalUnitCount: Int64
) Initializes an instance of
Progress
with a parent ofProgress.current()
.convenience init(totalUnitCount: Int64, parent: Progress, pendingUnitCount: Int64
) Initializes an instance of
Progress
with the specified total unit count, parent, and pending unit count.class func current(
) -> Progress? The instance of
Progress
associated with the current thread by a previous invocation ofbecomeCurrent(withPendingUnitCount:)
, if any.class func discreteProgress(totalUnitCount: Int64
) -> Progress Initializes an instance of
Progress
with anil
parent.var cancellationHandler: (() -> Void)?
A closure to be called when
cancel
is called.var completedUnitCount: Int64
The size of the job whose progress is being reported.
var fractionCompleted: Double
The fraction of the overall work completed by this progress object, including work done by any children it may have.
var isCancellable: Bool
Whether the work being done can be cancelled.
var isCancelled: Bool
Whether the work being done has been cancelled.
var isFinished: Bool
Returns
true
if the progress is finished.var isIndeterminate: Bool
Returns
true
if the progress is indeterminate.var isPausable: Bool
Whether the work being done can be paused.
var isPaused: Bool
Whether the work being done has been paused.
var kind: ProgressKind?
Optionally specifies more information about what kind of progress is being reported.
var localizedAdditionalDescription: String!
A more specific description of what progress is being made, fit to present to the user.
var localizedDescription: String!
A description of what progress is being made, fit to present to the user.
var pausingHandler: (() -> Void)?
A closure to be called when pause is called.
var resumingHandler: (() -> Void)?
A closure to be called when resume is called.
var totalUnitCount: Int64
How much of the job has been completed so far.
var userInfo: [ProgressUserInfoKey : Any]
A collection of arbitrary values associated with this
Progress
.func addChild(Progress, withPendingUnitCount: Int64
) Directly add a child progress to the receiver, assigning it a portion of the receiver’s total unit count.
func becomeCurrent(withPendingUnitCount: Int64
) Make this
Progress
the current thread’s current progress object, returned byProgress.currentProgress()
.func cancel(
) Calls the closure registered with the
cancellationHandler
property, if there is one, and set theisCancelled
property totrue
.func pause(
) Calls the closure registered with the
pausingHandler
property, if there is one, and set theisPaused
property totrue
.func resignCurrent(
) Balance the most recent previous invocation of
becomeCurrent(withPendingUnitCount:)
on the same thread.func resume(
) Calls the closure registered with the
resumingHandler
property, if there is one, and set theisPaused
property tofalse
.func setUserInfoObject(Any
?, forKey: ProgressUserInfoKey) Set a value in the dictionary returned by
userInfo
, with appropriate KVO notification for properties whose values can depend on values in the user info dictionary, likelocalizedDescription
. If a nil value is passed then the dictionary entry is removed.struct FileOperationKind