diff --git a/Sources/Documentation.docc/MigrationGuide/Migration-To-8.md b/Sources/Documentation.docc/MigrationGuide/Migration-To-8.md index c26a0c7c8..1fe167cc3 100644 --- a/Sources/Documentation.docc/MigrationGuide/Migration-To-8.md +++ b/Sources/Documentation.docc/MigrationGuide/Migration-To-8.md @@ -198,4 +198,50 @@ let colorElement = Filter.ColorElement(brightness, contrast, saturation, inputEV #### `DownloadTask` -`DownloadTask` has been redefined as a `class` instead of a `struct`, without further API modifications. +`DownloadTask` has been redefined as a `class` instead of a `struct`. + +For `ImageDownloader.download` methods that previously returned optional `DownloadTask`` values, now return non-optional values instead. For example: + +```swift +// old +open func downloadImage( + with url: URL, + options: KingfisherParsedOptionsInfo, + completionHandler: (@Sendable (Result) -> Void)? = nil +) -> DownloadTask? + +// new +open func downloadImage( + with url: URL, + options: KingfisherParsedOptionsInfo, + completionHandler: (@Sendable (Result) -> Void)? = nil +) -> DownloadTask +``` + +To check if a download task is valid, instead of checking `nil`, use `isInitialized` instead: + +```swift +// old +let downloadTask: DownloadTask? = downloader.downloadImage(with: url, options: options) + +func doSomethingWithTask() { + if let task = downloadTask { + // Do something with the task, for example, cancel it + } +} + +// new +let downloadTask: DownloadTask = downloader.downloadImage(with: url, options: options) + +func doSomethingWithTask() { + if downloadTask.isInitialized { + // Do something with the task, for example, cancel it + } +} +``` + +##### Cancel Token of DownloadTask + +In the current implementation, the cancel token of a `DownloadTask` is an optional value, meaning it does not exist until the download task has actually started. + +Typically, there is no need to interact directly with the cancel token; you can simply invoke the `cancel()` method to terminate an ongoing download task. diff --git a/Sources/Networking/ImageDownloader.swift b/Sources/Networking/ImageDownloader.swift index c2c3e8465..65cd03bc6 100644 --- a/Sources/Networking/ImageDownloader.swift +++ b/Sources/Networking/ImageDownloader.swift @@ -522,7 +522,7 @@ open class ImageDownloader: @unchecked Sendable { with url: URL, options: KingfisherOptionsInfo? = nil, progressBlock: DownloadProgressBlock? = nil, - completionHandler: (@Sendable (Result) -> Void)? = nil) -> DownloadTask? + completionHandler: (@Sendable (Result) -> Void)? = nil) -> DownloadTask { var info = KingfisherParsedOptionsInfo(options) if let block = progressBlock {