APIs and features overview:
- Downloader : Download a single file ‣ Download files
- Events Handler : Delegate ‣ Callbacks
- Post-processing : Adding post-process ‣ Poss-process Delegate ‣ Post-process Callbacks ‣ Creating Post-process
- Advanced Features : Configurations ‣ File Storage
// 1. Create a DMSwift instance with the cache directory path
let downloadManager = DMSwift(path: "features/content")
// 2. Start download a file
downloadManager.download(url) { fileData in
guard fileData.error == nil, path = fileData.location?.path else { return }
print("file location path: \(path)")
}
// 1. Create a DMSwift instance with the cache directory path
let downloadManager = DMSwift(path: "features/content")
// 2. Use callbacks
downloadManager.onDownloadComplete = { [weak self] in
self?.showDownloadSuccessAlert()
}
downloadManager.onDownloadCompletedWithError = { [weak self] failedTasks in
print("failed to download tasks: \(failedTasks)")
}
// 3. Start downloads
downloadManager.download(urls)
See Events Handler to learn more about available delegates and callbacks.
To handler events, you can use a delegate or callbacks.
Use downloadProgressDelegate
to receive events related to downloads.
To use a delegate:
class ViewController: UIViewController {
// .zip files location directory path
private var zipDirectory: String = "content/zip"
// Download Manager
private lazy var zipDownloadManager: DMSwift = {
let downloadManager = DMSwift(path: zipDirectory)
// Set downloadProgressDelegate to self to start use a delegate
downloadManager.downloadProgressDelegate = self
return downloadManager
}()
}
extension ViewController: DownloaderProgressDelegate {
…
}
Informs when download operations started.
func downloadStarted()
Informs when all download operations finished successfully.
func downloadDidComplete()
Informs when downloaded operations finished and some of them have errors.
func downloadDidCompletedWithError(tasks: [DMSwiftTypealias.Download.FailedTask])
Informs �when downloaded size or total files size changes.
func downloadDidUpdate(progress: Float, downloadedSize: Int64, totalSize: Int64)
Informs on changes in download task count progress.
func downloadDidUpdate(progress: Float, finishedTaskCount: Int64, taskCount: Int64)
Using completion
callback at download
method, when downloading a single file:
downloadManager.download(url) { fileData in
guard fileData.error == nil, path = fileData.location?.path else { return }
print("file location path: \\(path)")
}
completion
handler atdownload
method called when one of the download operations is finish in the queue.
Using onDownloadComplete
callback:
// called when all downloads complete successfully in the queue
downloadManager.onDownloadComplete = { [weak self] in
print("download completed")
}
Using onDownloadCompletedWithError
callback:
// called when all downloads complete in the queue, but some of them finished with error.
downloadManager.onDownloadCompletedWithError = { [weak self] failedTasks in
print("failed to download tasks: \(failedTasks)")
}
Using onDownloadStarted
callback:
// Used to inform that one of the downloads started in queue.
downloadManager.onDownloadStarted = { [weak self] in
print("download started")
}
Using onDownloadUpdateTaskCount
callback:
// Informs on changes in download task count progress.
downloadManager.onDownloadUpdateTaskCount = { [weak self] taskProgress in
print(“task progress percent: \(taskProgress.progress*100)%”)
}
Using onDownloadUpdateSize
callback:
// Informs �when downloaded size or total files size changes.
downloadManager.onDownloadUpdateSize = { [weak self] sizeProgress in
print(“task progress percent: \(sizeProgress.progress*100)%”)
}
DMSwift allows you to add post-processing to downloaded files. For example, using DMSwiftZipPostProcessing
, you can unzip a .zip file after the file is downloaded.
An example:
// creates a .zip processing
let zipProcessing = DMSwiftZipPostProcessing()
// creates download manager with post-process
let downloadManager = DMSwift(path: directory, postProcessings: [zipProcessing])
// start downloads
downloadManager.download([zipFileURLs])
DMSwift
doesn’t includeDMSwiftZipPostProcessing
or other post-processes, however, it’s possible to create own post-process or use one of the available. See how post-process works to learn more.
An example, how to start using delegate for post-processing:
// create download manager
…
// set delegate
downloadManager.postProcessDelegate = self
// uses post-process delegate methods
extension ViewController: PostProcessDelegate {
…
}
Informs when post-processing operations started.
func postProcessStarted()
Informs when all post-processing operations finished successfully.
func postProcessDidComplete()
Informs when post-processing operations finished and some of them have errors.
func postProcessDidCompletedWithError(tasks: [DMSwiftTypealias.PostProcess.FailedTask])
Informs on changes in post-process operations count progress.
func postProcessDidUpdate(progress: Float, finishedTaskCount: Int64, taskCount: Int64)
Using onPostProcessStarted
callback:
// Used to inform that one of the post-processes started in queue.
downloadManager.onPostProcessStarted = { [weak self] in
print(“post-process started")
}
Using onPostProcessCompleted
callback:
// called when all post-processes complete successfully in the queue.
downloadManager.onPostProcessCompleted = { [weak self] in
print(“post processes completed")
}
Using onProcessCompletedWithError
callback:
// called when all post-processes complete in the queue, but some of them finished with error.
downloadManager.onProcessCompletedWithError = { [weak self] postProcessFailedTask in
print("failed to post-process tasks: \(postProcessFailedTask)")
}
Using onPostProcessUpdateTaskCount
callback:
// Reports on changes in post-process task count progress.
downloadManager.onPostProcessUpdateTaskCount = { [weak self] taskProgress in
print(“task progress percent: \(taskProgress.progress*100)%”)
}
Use PostProcessing
protocol to create your own post-process for downloaded files.
See how post-process works to learn more.
See available post-process to learn more.
You can modify the DMSwift
configuration directly changing the configuration
parameters or create your own configuration inheriting from the DMSwiftConfiguration
protocol
and assign your created configuration during initialization or through the update method.
Consider the following example:
// initializes DMSwift
let downloadManager = DMSwift(path: directoryPath)
// Updates max concurrent downloads count
let downloadManager.configuration.downloadMaxConcurrentOperationCount = 10
The configuration makes it possible to configure parameters such as maximum download operations that should start concurrently, download queue QoS type, type of session task, and so on.
See
DMSwiftConfiguration
at API documentaion to learn more.
See DMSwift in-depth to learn more about how different configurations can effect to work.
If you need access to files, you can use the FileStorage
. All methods are Public
for use.
See
FileReading
,FileWriting
protocols
API documentaion for more info.