Skip to content

Commit

Permalink
Merge pull request #194 from onevcat/feature/priority-control
Browse files Browse the repository at this point in the history
Options API adopting
  • Loading branch information
onevcat committed Jan 6, 2016
2 parents 3d9c80d + 0b0e965 commit 96a3553
Show file tree
Hide file tree
Showing 13 changed files with 338 additions and 349 deletions.
14 changes: 6 additions & 8 deletions Kingfisher.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@
D13F49F41BEDA8B300CE335D /* ImageCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1ED2D571AD2D0F900CFC3EB /* ImageCache.swift */; };
D13F49F51BEDA8B300CE335D /* ImageDownloader.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1ED2D581AD2D0F900CFC3EB /* ImageDownloader.swift */; };
D13F49F61BEDA8B300CE335D /* KingfisherManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1ED2D591AD2D0F900CFC3EB /* KingfisherManager.swift */; };
D13F49F71BEDA8B300CE335D /* KingfisherOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1ED2D5A1AD2D0F900CFC3EB /* KingfisherOptions.swift */; };
D13F49F81BEDA8B300CE335D /* ImageTransition.swift in Sources */ = {isa = PBXBuildFile; fileRef = D11250C01BAC4B6300B986EF /* ImageTransition.swift */; };
D13F49F91BEDA8B300CE335D /* KingfisherOptionsInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B412CA41AE8A2F9008D530A /* KingfisherOptionsInfo.swift */; };
D13F49FA1BEDA8B300CE335D /* Resource.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B2C4DF71B7D7ACD000912CA /* Resource.swift */; };
D13F49FB1BEDA8B300CE335D /* ThreadHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1B1B98F1BC7EB7100DE20D8 /* ThreadHelper.swift */; };
D13F49FE1BEDBB4A00CE335D /* CollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BAFBA3C1AD671E400FB0300 /* CollectionViewCell.swift */; };
D13F49FF1BEDBB4A00CE335D /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1ED2D121AD2CFA600CFC3EB /* ViewController.swift */; };
D151E72B1AD3C48D004FD4AE /* UIImage+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = D151E72A1AD3C48D004FD4AE /* UIImage+Extension.swift */; };
D17363CB1C3AABF80042F42E /* KingfisherOptionsInfoTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D17363CA1C3AABF80042F42E /* KingfisherOptionsInfoTests.swift */; };
D1A7B6CA1BDBC9C60083A684 /* ImageExtensionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1A7B6C91BDBC9C60083A684 /* ImageExtensionTests.swift */; };
D1A7B6CC1BDBEC090083A684 /* dancing-banana.gif in Resources */ = {isa = PBXBuildFile; fileRef = D1A7B6CB1BDBEC090083A684 /* dancing-banana.gif */; };
D1B1B9901BC7EB7100DE20D8 /* ThreadHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1B1B98F1BC7EB7100DE20D8 /* ThreadHelper.swift */; };
Expand All @@ -58,7 +58,6 @@
D1ED2D5D1AD2D0F900CFC3EB /* ImageCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1ED2D571AD2D0F900CFC3EB /* ImageCache.swift */; };
D1ED2D5E1AD2D0F900CFC3EB /* ImageDownloader.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1ED2D581AD2D0F900CFC3EB /* ImageDownloader.swift */; };
D1ED2D5F1AD2D0F900CFC3EB /* KingfisherManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1ED2D591AD2D0F900CFC3EB /* KingfisherManager.swift */; };
D1ED2D601AD2D0F900CFC3EB /* KingfisherOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1ED2D5A1AD2D0F900CFC3EB /* KingfisherOptions.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -153,6 +152,7 @@
D13F49D81BEDA67C00CE335D /* Kingfisher-tvOS.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Kingfisher-tvOS.h"; sourceTree = "<group>"; };
D13F49DA1BEDA67C00CE335D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
D151E72A1AD3C48D004FD4AE /* UIImage+Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIImage+Extension.swift"; sourceTree = "<group>"; };
D17363CA1C3AABF80042F42E /* KingfisherOptionsInfoTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KingfisherOptionsInfoTests.swift; sourceTree = "<group>"; };
D1A7B6C91BDBC9C60083A684 /* ImageExtensionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImageExtensionTests.swift; sourceTree = "<group>"; };
D1A7B6CB1BDBEC090083A684 /* dancing-banana.gif */ = {isa = PBXFileReference; lastKnownFileType = image.gif; path = "dancing-banana.gif"; sourceTree = "<group>"; };
D1B1B98F1BC7EB7100DE20D8 /* ThreadHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ThreadHelper.swift; sourceTree = "<group>"; };
Expand All @@ -173,7 +173,6 @@
D1ED2D571AD2D0F900CFC3EB /* ImageCache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImageCache.swift; sourceTree = "<group>"; };
D1ED2D581AD2D0F900CFC3EB /* ImageDownloader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImageDownloader.swift; sourceTree = "<group>"; };
D1ED2D591AD2D0F900CFC3EB /* KingfisherManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KingfisherManager.swift; sourceTree = "<group>"; };
D1ED2D5A1AD2D0F900CFC3EB /* KingfisherOptions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KingfisherOptions.swift; sourceTree = "<group>"; };
D809C0611AAB7CA1AE240862 /* Pods-KingfisherTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-KingfisherTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-KingfisherTests/Pods-KingfisherTests.debug.xcconfig"; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -253,11 +252,11 @@
D1ED2D021AD2CFA600CFC3EB = {
isa = PBXGroup;
children = (
D1ED2D361AD2D09F00CFC3EB /* Kingfisher */,
D13F49D71BEDA67C00CE335D /* Kingfisher-tvOS */,
D1ED2D0D1AD2CFA600CFC3EB /* Kingfisher-Demo */,
D13F49C31BEDA53F00CE335D /* Kingfisher-tvOS-Demo */,
D1ED2D361AD2D09F00CFC3EB /* Kingfisher */,
D1ED2D451AD2D09F00CFC3EB /* KingfisherTests */,
D13F49D71BEDA67C00CE335D /* Kingfisher-tvOS */,
D1ED2D0C1AD2CFA600CFC3EB /* Products */,
9C068B80BAB8294A1732ADC5 /* Pods */,
EA99D30544BD22799F7A5367 /* Frameworks */,
Expand Down Expand Up @@ -309,7 +308,6 @@
D1ED2D571AD2D0F900CFC3EB /* ImageCache.swift */,
D1ED2D581AD2D0F900CFC3EB /* ImageDownloader.swift */,
D1ED2D591AD2D0F900CFC3EB /* KingfisherManager.swift */,
D1ED2D5A1AD2D0F900CFC3EB /* KingfisherOptions.swift */,
D11250C01BAC4B6300B986EF /* ImageTransition.swift */,
4B412CA41AE8A2F9008D530A /* KingfisherOptionsInfo.swift */,
4B2C4DF71B7D7ACD000912CA /* Resource.swift */,
Expand Down Expand Up @@ -339,6 +337,7 @@
4BBA04C31AD7986100A5CF82 /* KingfisherTestHelper.swift */,
4B5083371BD88C3E00AEA807 /* KingfisherManagerTests.swift */,
D1A7B6C91BDBC9C60083A684 /* ImageExtensionTests.swift */,
D17363CA1C3AABF80042F42E /* KingfisherOptionsInfoTests.swift */,
);
path = KingfisherTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -643,7 +642,6 @@
D13F49F41BEDA8B300CE335D /* ImageCache.swift in Sources */,
D13F49F51BEDA8B300CE335D /* ImageDownloader.swift in Sources */,
D13F49F61BEDA8B300CE335D /* KingfisherManager.swift in Sources */,
D13F49F71BEDA8B300CE335D /* KingfisherOptions.swift in Sources */,
D13F49F81BEDA8B300CE335D /* ImageTransition.swift in Sources */,
D13F49F91BEDA8B300CE335D /* KingfisherOptionsInfo.swift in Sources */,
D13F49FA1BEDA8B300CE335D /* Resource.swift in Sources */,
Expand Down Expand Up @@ -674,7 +672,6 @@
D1B1B9901BC7EB7100DE20D8 /* ThreadHelper.swift in Sources */,
4B412CA51AE8A2F9008D530A /* KingfisherOptionsInfo.swift in Sources */,
4B2C4DF81B7D7ACD000912CA /* Resource.swift in Sources */,
D1ED2D601AD2D0F900CFC3EB /* KingfisherOptions.swift in Sources */,
D11250C11BAC4B6300B986EF /* ImageTransition.swift in Sources */,
D151E72B1AD3C48D004FD4AE /* UIImage+Extension.swift in Sources */,
);
Expand All @@ -684,6 +681,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
D17363CB1C3AABF80042F42E /* KingfisherOptionsInfoTests.swift in Sources */,
4B82DDFC1AD7701900074995 /* ImageCacheTests.swift in Sources */,
4BBA04C21AD795C500A5CF82 /* ImageDownloaderTests.swift in Sources */,
4B6D4F651AE0A46D0084D15B /* UIImageViewExtensionTests.swift in Sources */,
Expand Down
52 changes: 25 additions & 27 deletions Kingfisher/ImageCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ extension ImageCache {

- returns: The retrieving task.
*/
public func retrieveImageForKey(key: String, options: KingfisherManager.Options, completionHandler: ((UIImage?, CacheType!) -> ())?) -> RetrieveImageDiskTask? {
public func retrieveImageForKey(key: String, options: KingfisherOptionsInfo?, completionHandler: ((UIImage?, CacheType!) -> ())?) -> RetrieveImageDiskTask? {
// No completion handler. Not start working and early return.
guard let completionHandler = completionHandler else {
return nil
Expand All @@ -273,10 +273,10 @@ extension ImageCache {
if let image = self.retrieveImageInMemoryCacheForKey(key) {

//Found image in memory cache.
if options.shouldDecode {
if let options = options where options.backgroundDecode {
dispatch_async(self.processQueue, { () -> Void in
let result = image.kf_decodedImage(scale: options.scale)
dispatch_async(options.queue, { () -> Void in
let result = image.kf_decodedImage(scale: options.scaleFactor)
dispatch_async(options.callbackDispatchQueue, { () -> Void in
completionHandler(result, .Memory)
})
})
Expand All @@ -286,38 +286,36 @@ extension ImageCache {
} else {
var sSelf: ImageCache! = self
block = dispatch_block_create(DISPATCH_BLOCK_INHERIT_QOS_CLASS) {

// Begin to load image from disk
dispatch_async(sSelf.ioQueue, { () -> Void in
if let image = sSelf.retrieveImageInDiskCacheForKey(key, scale: options.scale) {
if options.shouldDecode {
dispatch_async(sSelf.processQueue, { () -> Void in
let result = image.kf_decodedImage(scale: options.scale)
sSelf.storeImage(result!, forKey: key, toDisk: false, completionHandler: nil)
let options = options ?? KingfisherEmptyOptionsInfo
if let image = sSelf.retrieveImageInDiskCacheForKey(key, scale: options.scaleFactor) {
if options.backgroundDecode {
dispatch_async(sSelf.processQueue, { () -> Void in
let result = image.kf_decodedImage(scale: options.scaleFactor)
sSelf.storeImage(result!, forKey: key, toDisk: false, completionHandler: nil)

dispatch_async(options.queue, { () -> Void in
completionHandler(result, .Memory)
sSelf = nil
})
})
} else {
sSelf.storeImage(image, forKey: key, toDisk: false, completionHandler: nil)
dispatch_async(options.queue, { () -> Void in
completionHandler(image, .Disk)
dispatch_async(options.callbackDispatchQueue, { () -> Void in
completionHandler(result, .Memory)
sSelf = nil
})
}
})
} else {
// No image found from either memory or disk
dispatch_async(options.queue, { () -> Void in
completionHandler(nil, nil)
sSelf.storeImage(image, forKey: key, toDisk: false, completionHandler: nil)
dispatch_async(options.callbackDispatchQueue, { () -> Void in
completionHandler(image, .Disk)
sSelf = nil
})
}
})
} else {
// No image found from either memory or disk
dispatch_async(options.callbackDispatchQueue, { () -> Void in
completionHandler(nil, nil)
sSelf = nil
})
}
}

dispatch_async(dispatch_get_main_queue(), block!)
dispatch_async(sSelf.ioQueue, block!)
}

return block
Expand All @@ -342,7 +340,7 @@ extension ImageCache {

- returns: The image object if it is cached, or `nil` if there is no such key in the cache.
*/
public func retrieveImageInDiskCacheForKey(key: String, scale: CGFloat = KingfisherManager.DefaultOptions.scale) -> UIImage? {
public func retrieveImageInDiskCacheForKey(key: String, scale: CGFloat = 1.0) -> UIImage? {
return diskImageForKey(key, scale: scale)
}
}
Expand Down
38 changes: 26 additions & 12 deletions Kingfisher/ImageDownloader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ public struct RetrieveImageDownloadTask {
public var URL: NSURL? {
return internalTask.originalRequest?.URL
}

/// The relative priority of this download task.
/// It represents the `priority` property of the internal `NSURLSessionTask` of this download task.
/// The value for it is between 0.0~1.0. Default priority is value of 0.5.
/// See documentation on `priority` of `NSURLSessionTask` for more about it.
public var priority: Float {
get {
return internalTask.priority
}
set {
internalTask.priority = newValue
}
}
}

private let defaultDownloaderName = "default"
Expand Down Expand Up @@ -90,8 +103,9 @@ public class ImageDownloader: NSObject {
class ImageFetchLoad {
var callbacks = [CallbackPair]()
var responseData = NSMutableData()
var shouldDecode = false
var scale = KingfisherManager.DefaultOptions.scale

var options: KingfisherOptionsInfo?

var downloadTaskCount = 0
var downloadTask: RetrieveImageDownloadTask?
}
Expand Down Expand Up @@ -176,21 +190,21 @@ public extension ImageDownloader {
progressBlock: ImageDownloaderProgressBlock?,
completionHandler: ImageDownloaderCompletionHandler?) -> RetrieveImageDownloadTask?
{
return downloadImageWithURL(URL, options: KingfisherManager.DefaultOptions, progressBlock: progressBlock, completionHandler: completionHandler)
return downloadImageWithURL(URL, options: nil, progressBlock: progressBlock, completionHandler: completionHandler)
}

/**
Download an image with a URL and option.

- parameter URL: Target URL.
- parameter options: The options could control download behavior. See `KingfisherManager.Options`
- parameter options: The options could control download behavior. See `KingfisherOptionsInfo`.
- parameter progressBlock: Called when the download progress updated.
- parameter completionHandler: Called when the download progress finishes.

- returns: A downloading task. You could call `cancel` on it to stop the downloading process.
*/
public func downloadImageWithURL(URL: NSURL,
options: KingfisherManager.Options,
options: KingfisherOptionsInfo?,
progressBlock: ImageDownloaderProgressBlock?,
completionHandler: ImageDownloaderCompletionHandler?) -> RetrieveImageDownloadTask?
{
Expand All @@ -203,7 +217,7 @@ public extension ImageDownloader {

internal func downloadImageWithURL(URL: NSURL,
retrieveImageTask: RetrieveImageTask?,
options: KingfisherManager.Options,
options: KingfisherOptionsInfo?,
progressBlock: ImageDownloaderProgressBlock?,
completionHandler: ImageDownloaderCompletionHandler?) -> RetrieveImageDownloadTask?
{
Expand Down Expand Up @@ -231,10 +245,9 @@ public extension ImageDownloader {
let dataTask = session.dataTaskWithRequest(request)

fetchLoad.downloadTask = RetrieveImageDownloadTask(internalTask: dataTask, ownerDownloader: self)
fetchLoad.shouldDecode = options.shouldDecode
fetchLoad.scale = options.scale
fetchLoad.options = options

dataTask.priority = options.lowPriority ? NSURLSessionTaskPriorityLow : NSURLSessionTaskPriorityDefault
dataTask.priority = options?.downloadPriority ?? NSURLSessionTaskPriorityDefault
dataTask.resume()
}

Expand Down Expand Up @@ -353,12 +366,13 @@ extension ImageDownloader: NSURLSessionDataDelegate {

if let fetchLoad = self.fetchLoadForKey(URL) {

if let image = UIImage.kf_imageWithData(fetchLoad.responseData, scale: fetchLoad.scale) {
let options = fetchLoad.options ?? KingfisherEmptyOptionsInfo
if let image = UIImage.kf_imageWithData(fetchLoad.responseData, scale: options.scaleFactor) {

self.delegate?.imageDownloader?(self, didDownloadImage: image, forURL: URL, withResponse: task.response!)

if fetchLoad.shouldDecode {
self.callbackWithImage(image.kf_decodedImage(scale: fetchLoad.scale), error: nil, imageURL: URL, originalData: fetchLoad.responseData)
if options.backgroundDecode {
self.callbackWithImage(image.kf_decodedImage(scale: options.scaleFactor), error: nil, imageURL: URL, originalData: fetchLoad.responseData)
} else {
self.callbackWithImage(image, error: nil, imageURL: URL, originalData: fetchLoad.responseData)
}
Expand Down
Loading

0 comments on commit 96a3553

Please sign in to comment.