DMSwift
may have several post-processes.
Post-processing can only be assigned during the initialization of DMSwift
. During initialization, DMSwift
initializes the PostProcessQueue
with a list of post-processes.
With each successfully downloaded file, PostProcessQueue
starts post-processing one by one.
During post-processing, the downloaded file is checked for the file extension, if post-processing can process this file, post-processing will then start. Over one file, several post-processing operations can be carried out in turn.
To create you need to go through 4 basic steps:
- Create your
struct
object that will be inherited from thePostProcessing
protocol - Specify supported file resolution
- Implement actions while receiving data for post-processing in the
prepare
method - Implement the post-processing process in the
process
method - Call
onComplete
after post-process is complete.
Let's say you want to create post-processing for unzipping a file:
public struct ZipProcessing: PostProcessing {
}
First you need to specify which file extension your post-processing will process:
public struct ZipProcessing: PostProcessing {
public var supportedFileExtensions: Set<String> = ["zip"]
}
Implement actions while receiving data for post-processing in the prepare
method:
public struct ZipProcessing: PostProcessing {
private var sourceLocation: URL?
private var fileManager: FileStorageManager?
public mutating func prepare(fileManager: FileStorageManager?, filename: String?, fileExtention: String?, sourceLocation: URL?) {
self.fileManager = fileManager
self.sourceLocation = sourceLocation
}
}
Implement the post-processing process in the process
method:
public struct ZipProcessing: PostProcessing {
private var sourceLocation: URL?
private var fileManager: FileStorageManager?
public func process() {
guard let fileManager = fileManager else { return }
guard let sourceLocation = sourceLocation else { return }
guard let directoryUrl = fileManager.directoryURL else { return }
// process logic
…
try? fileManager.fileManager.unzipItem(at: sourceLocation, to: directoryUrl, progress: progress)
}
}
Call onComplete
after post-process is complete:
public struct ZipProcessing: PostProcessing {
private var sourceLocation: URL?
private var fileManager: FileStorageManager?
public func process() {
var error: Error? = nil
// process logic
…
onComplete?(error)
}
}