Skip to content

Commit

Permalink
Public task properties
Browse files Browse the repository at this point in the history
  • Loading branch information
onevcat committed Dec 30, 2015
1 parent e3f7155 commit aa6050f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
7 changes: 6 additions & 1 deletion Kingfisher/ImageDownloader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,16 @@ public typealias ImageDownloaderCompletionHandler = ((image: UIImage?, error: NS
/// Download task.
public struct RetrieveImageDownloadTask {
let internalTask: NSURLSessionDataTask
weak var ownerDownloader: ImageDownloader?

public private(set) weak var ownerDownloader: ImageDownloader?

public func cancel() {
ownerDownloader?.cancelDownloadingTask(self)
}

public var URL: NSURL? {
return internalTask.originalRequest?.URL
}
}

private let defaultDownloaderName = "default"
Expand Down
4 changes: 2 additions & 2 deletions Kingfisher/KingfisherManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public class RetrieveImageTask {
// the download task should not begin.
var cancelledBeforeDownlodStarting: Bool = false

var diskRetrieveTask: RetrieveImageDiskTask?
var downloadTask: RetrieveImageDownloadTask?
public var diskRetrieveTask: RetrieveImageDiskTask?
public var downloadTask: RetrieveImageDownloadTask?

/**
Cancel current task. If this task does not begin or already done, do nothing.
Expand Down
24 changes: 18 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!

pod 'Kingfisher', '~> 1.8'
pod 'Kingfisher', '~> 1.9'
```

Then, run the following command:
Expand All @@ -83,7 +83,7 @@ $ brew install carthage
To integrate Kingfisher into your Xcode project using Carthage, specify it in your `Cartfile`:

``` ogdl
github "onevcat/Kingfisher" ~> 1.8
github "onevcat/Kingfisher" ~> 1.9
```

Then, run the following command to build the Kingfisher framework:
Expand Down Expand Up @@ -215,16 +215,28 @@ imageView.kf_setImageWithURL(NSURL(string: "your_image_url")!,

#### Cancel Task

All `kf_setImageWithURL` methods return a `RetrieveImageTask` object. You can `cancel` the task if the images are not needed.
You can `cancel` the task if the images are not needed anymore.
It could be useful when you use Kingfisher to set an image in a cell of table view or collection view,
but users scroll the view and the cells disappeared before downloading finishing.

``` swift
let task = imageView.kf_setImageWithURL(NSURL(string: "http://your_image_url.png")!)
task.cancel()
imageView.kf_setImageWithURL(NSURL(string: "http://your_image_url.png")!)

// The image retrieving will stop.
imageView.kf_cancelDownloadTask()
```

There is a category for `UIButton` as well.
All `kf_setImageWithURL` methods return a `RetrieveImageTask` object as well. You can also hold and manage it if you need to apply some check:

``` swift
let task = imageView.kf_setImageWithURL(NSURL(string: "http://your_image_url.png")!)

let urlShouldNotBeCancelled: URL = ...

if task.downloadTask?.URL != urlShouldNotBeCancelled {
task.cancel()
}
```

### Downloader & Cache system

Expand Down

0 comments on commit aa6050f

Please sign in to comment.