Skip to content

Commit

Permalink
Fix download image return value and update migration guide
Browse files Browse the repository at this point in the history
  • Loading branch information
onevcat committed Jun 14, 2024
1 parent 4b7e022 commit a5878df
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
48 changes: 47 additions & 1 deletion Sources/Documentation.docc/MigrationGuide/Migration-To-8.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<ImageLoadingResult, KingfisherError>) -> Void)? = nil
) -> DownloadTask?

// new
open func downloadImage(
with url: URL,
options: KingfisherParsedOptionsInfo,
completionHandler: (@Sendable (Result<ImageLoadingResult, KingfisherError>) -> 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.
2 changes: 1 addition & 1 deletion Sources/Networking/ImageDownloader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ open class ImageDownloader: @unchecked Sendable {
with url: URL,
options: KingfisherOptionsInfo? = nil,
progressBlock: DownloadProgressBlock? = nil,
completionHandler: (@Sendable (Result<ImageLoadingResult, KingfisherError>) -> Void)? = nil) -> DownloadTask?
completionHandler: (@Sendable (Result<ImageLoadingResult, KingfisherError>) -> Void)? = nil) -> DownloadTask
{
var info = KingfisherParsedOptionsInfo(options)
if let block = progressBlock {
Expand Down

0 comments on commit a5878df

Please sign in to comment.