Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature - New ImageDownloader Initializer Accepting Manager #32

Merged
merged 1 commit into from
Oct 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 34 additions & 18 deletions Source/ImageDownloader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,23 @@ public class ImageDownloader {
/// The credential used for authenticating each download request.
public private(set) var credential: NSURLCredential?

var queuedRequests: [Request]
var activeRequestCount: Int
let sessionManager: Alamofire.Manager
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docs?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't generally put docstrings on anything other than public properties unless they aren't self documenting.

let downloadPrioritization: DownloadPrioritization
let maximumActiveDownloads: Int

let sessionManager: Alamofire.Manager
var activeRequestCount = 0
var queuedRequests: [Request] = []
var responseHandlers: [String: ResponseHandler] = [:]

private let synchronizationQueue: dispatch_queue_t
private let responseQueue: dispatch_queue_t
private let downloadPrioritization: DownloadPrioritization
private let synchronizationQueue: dispatch_queue_t = {
let name = String(format: "com.alamofire.imagedownloader.synchronizationqueue-%08%08", arc4random(), arc4random())
return dispatch_queue_create(name, DISPATCH_QUEUE_SERIAL)
}()

private var responseHandlers: [String: ResponseHandler]
private let responseQueue: dispatch_queue_t = {
let name = String(format: "com.alamofire.imagedownloader.responsequeue-%08%08", arc4random(), arc4random())
return dispatch_queue_create(name, DISPATCH_QUEUE_CONCURRENT)
}()

// MARK: - Initialization

Expand Down Expand Up @@ -161,21 +167,31 @@ public class ImageDownloader {
self.downloadPrioritization = downloadPrioritization
self.maximumActiveDownloads = maximumActiveDownloads
self.imageCache = imageCache
}

self.queuedRequests = []
self.responseHandlers = [:]
/**
Initializes the `ImageDownloader` instance with the given sesion manager, download prioritization, maximum
active download count and image cache.

self.activeRequestCount = 0
- parameter sessionManager: The Alamofire `Manager` instance to handle all download requests.
- parameter downloadPrioritization: The download prioritization of the download queue. `.FIFO` by default.
- parameter maximumActiveDownloads: The maximum number of active downloads allowed at any given time.
- parameter imageCache: The image cache used to store all downloaded images in.

self.synchronizationQueue = {
let name = String(format: "com.alamofire.imagedownloader.synchronizationqueue-%08%08", arc4random(), arc4random())
return dispatch_queue_create(name, DISPATCH_QUEUE_SERIAL)
}()
- returns: The new `ImageDownloader` instance.
*/
public init(
sessionManager: Manager,
downloadPrioritization: DownloadPrioritization = .FIFO,
maximumActiveDownloads: Int = 4,
imageCache: ImageRequestCache? = AutoPurgingImageCache())
{
self.sessionManager = sessionManager
self.sessionManager.startRequestsImmediately = false

self.responseQueue = {
let name = String(format: "com.alamofire.imagedownloader.responsequeue-%08%08", arc4random(), arc4random())
return dispatch_queue_create(name, DISPATCH_QUEUE_CONCURRENT)
}()
self.downloadPrioritization = downloadPrioritization
self.maximumActiveDownloads = maximumActiveDownloads
self.imageCache = imageCache
}

// MARK: - Authentication
Expand Down
11 changes: 11 additions & 0 deletions Tests/ImageDownloaderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@ class ImageDownloaderTestCase: BaseTestCase {
XCTAssertNil(downloader, "downloader should be nil")
}

func testThatImageDownloaderCanBeInitializedWithManagerInstanceAndDeinitialized() {
// Given
var downloader: ImageDownloader? = ImageDownloader(sessionManager: Manager())

// When
downloader = nil

// Then
XCTAssertNil(downloader, "downloader should be nil")
}

func testThatImageDownloaderCanBeInitializedAndDeinitializedWithActiveDownloads() {
// Given
var downloader: ImageDownloader? = ImageDownloader()
Expand Down