Controlling Progress children by adding remove – onlinecode
In this post we will give you information about Controlling Progress children by adding remove – onlinecode. Hear we will give you detail about Controlling Progress children by adding remove – onlinecodeAnd how to use it also give you demo for it if it is necessary.
Controlling Progress children by default makes it only easy to add children to a Progress instance, but removing is not possible by default. This could have been useful when you want to use a single Progress
instance which can have different children over time. Using a custom class MutableProgress makes this possible.
Controlling Progress children by subclassing Progress
The NSProgress
instance which is renamed to Progress
since Swift 3.0, is the main class to use for progress reporting in Swift. By managing the children by ourselves we’re able to control the children and add the possibility to remove children.
// Remove the Video upload progress, as the video file has been deleted by the user during upload.
totalUploadProgress.removeChild(videoUploadProgress)
By doing so, we need to make sure updates are broadcasted correctly. Therefore we need to override the properties fractionCompleted
, totalUnitCount
and completedUnitCount
. Progress
is using key value observation internally. Using willChange
and didChange
adds compatibility with UI elements like the UIProgressView
.
/// Removes the given child from the progress reporting.
///
/// - Parameter child: The child to remove.
func removeChild(_ child: Progress) {
willChangeValue(for: .fractionCompleted)
willChangeValue(for: .completedUnitCount)
willChangeValue(for: .totalUnitCount)
children.removeValue(forKey: child)?.invalidate()
didChangeValue(for: .totalUnitCount)
didChangeValue(for: .completedUnitCount)
didChangeValue(for: .fractionCompleted)
}
Checking if the progress is completed
By using a simple extension it is easy to verify if a Progress
instance is completed.
private extension Progress {
var isCompleted: Bool {
guard totalUnitCount > 0 else { return true }
return completedUnitCount >= totalUnitCount
}
}
Using the code in a playground
You can try it out easily using a playground and including the MutableProgress
there. You can find the custom MutableProgress
class here.
let totalUploadProgress = MutableProgress()
let imageUploadProgress = Progress(totalUnitCount: 1)
let videoUploadProgress = Progress(totalUnitCount: 1)
totalUploadProgress.addChild(imageUploadProgress)
totalUploadProgress.addChild(videoUploadProgress)
// Mark Image uploading as completed
imageUploadProgress.completedUnitCount = 1
print(totalUploadProgress.fractionCompleted) // 0.5
print(totalUploadProgress.isCompleted) // false
// Remove the Video upload progress, as the video file has been deleted by the user during upload.
totalUploadProgress.removeChild(videoUploadProgress)
print(totalUploadProgress.fractionCompleted) // 1.0
print(totalUploadProgress.isCompleted) // true
Hope this code and post will helped you for implement Controlling Progress children by adding remove – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs