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

Add a method to enqueue multiple image downloads. #51

Closed
wants to merge 2 commits into from
Closed
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
34 changes: 34 additions & 0 deletions Source/ImageDownloader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,40 @@ public class ImageDownloader {

return nil
}

/**
Creates a download request using the internal Alamofire `Manager` instance for each specified URL request.
The `filter` and `completion` parameters are attached to each 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
34 changes: 34 additions & 0 deletions Tests/ImageDownloaderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,40 @@ class ImageDownloaderTestCase: BaseTestCase {
XCTAssertTrue(result1?.isSuccess ?? false, "result 1 should be a success case")
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
Expand Down