Skip to content

Commit

Permalink
ImageDownloader can now enqueue multiple image downloads at once.
Browse files Browse the repository at this point in the history
  • Loading branch information
SlaunchaMan authored and cnoon committed Nov 22, 2015
1 parent d1e3415 commit 50e2725
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
31 changes: 31 additions & 0 deletions Source/ImageDownloader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,37 @@ public class ImageDownloader {
return nil
}

/**
Creates a download request using the internal Alamofire `Manager` instance for each specified URL request.

For each request, if the same download request is already in the queue or currently being downloaded, the
filter and completion handler are appended to the already existing request. Once the request completes, all
filters and completion handlers attached to the request are executed in the order they were added.
Additionally, any filters attached to the request with the same identifiers are only executed once. The
resulting image is then passed into each completion handler paired with the filter.

You should not attempt to directly cancel any of the `request`s inside the request receipts array since other
callers may be relying on the completion of that request. Instead, you should call
`cancelRequestForRequestReceipt` with the returned request receipt to allow the `ImageDownloader` to optimize
the cancellation on behalf of all active callers.

- parameter URLRequests: The URL requests.
- parameter filter The image filter to apply to the image after each download is complete.
- parameter completion: The closure called when each download request is complete.

- returns: The request receipts for the download requests if available. If an image is stored in the image
cache and the URL request cache policy allows the cache to be used, a receipt will not be returned
for that request.
*/
public func downloadImages(
URLRequests URLRequests: [URLRequestConvertible],
filter: ImageFilter? = nil,
completion: CompletionHandler? = nil)
-> [RequestReceipt]
{
return URLRequests.flatMap { downloadImage(URLRequest: $0, filter: filter, completion: completion) }
}

/**
Cancels the request in the receipt by removing the response handler and cancelling the request if necessary.

Expand Down
31 changes: 31 additions & 0 deletions Tests/ImageDownloaderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,37 @@ class ImageDownloaderTestCase: BaseTestCase {
XCTAssertTrue(result2?.isSuccess ?? false, "result 2 should be a success case")
}

func testThatItCanEnqueueMultipleImages() {
// Given
let downloader = ImageDownloader()

let download1 = URLRequest(.GET, "https://httpbin.org/image/jpeg")
let download2 = URLRequest(.GET, "https://httpbin.org/image/png")

let expectation = expectationWithDescription("both downloads should succeed")
var completedDownloads = 0

var results: [Result<Image, NSError>] = []

// When
downloader.downloadImages(URLRequests: [download1, download2], filter: nil) { closureResponse in
results.append(closureResponse.result)

++completedDownloads
if completedDownloads == 2 { expectation.fulfill() }
}

let activeRequestCount = downloader.activeRequestCount

waitForExpectationsWithTimeout(timeout, handler: nil)

// Then
XCTAssertEqual(activeRequestCount, 2, "active request count should be 2")

XCTAssertTrue(results[0].isSuccess, "the first result should be a success case")
XCTAssertTrue(results[1].isSuccess, "the second result should be a success case")
}

func testThatItDoesNotExceedTheMaximumActiveDownloadsLimit() {
// Given
let downloader = ImageDownloader(maximumActiveDownloads: 1)
Expand Down

0 comments on commit 50e2725

Please # to comment.