Skip to content

Commit

Permalink
some refactoring and additional logging
Browse files Browse the repository at this point in the history
  • Loading branch information
azarovalex committed Jan 22, 2024
1 parent a432412 commit 7caca3a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
2 changes: 2 additions & 0 deletions Sources/MapboxNavigation/ImageDownload.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ final class ImageDownloadOperation: Operation, ImageDownload {

override func start() {
withLock {
print("[imagedownloader] operation started for url: \(self.request.url!.absoluteString)")
guard !isCancelled && state != .finished else {
state = .finished;
return
Expand Down Expand Up @@ -172,6 +173,7 @@ final class ImageDownloadOperation: Operation, ImageDownload {
}
}

assert(completions.count > 0, "ImageDownloadOperation should have at least one completion")
// The motivation is to call completions outside the lock to reduce the likehood of a deadlock.
for completion in completions {
completion(completionData.0, completionData.1)
Expand Down
15 changes: 7 additions & 8 deletions Sources/MapboxNavigation/ImageDownloader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ typealias CachedResponseCompletionHandler = (CachedURLResponse?, Error?) -> Void
typealias ImageDownloadCompletionHandler = (DownloadError?) -> Void

protocol ReentrantImageDownloader {
func download(with url: URL, completion: CachedResponseCompletionHandler?) -> Void
func download(with url: URL, completion: @escaping CachedResponseCompletionHandler) -> Void
func activeOperation(with url: URL) -> ImageDownload?
func setOperationType(_ operationType: ImageDownload.Type?)
}
Expand Down Expand Up @@ -43,22 +43,21 @@ class ImageDownloader: NSObject, ReentrantImageDownloader, URLSessionDataDelegat
urlSession.invalidateAndCancel()
}

func download(with url: URL, completion: CachedResponseCompletionHandler?) {
func download(with url: URL, completion: @escaping CachedResponseCompletionHandler) {
print("$$$ asked to download an image with url: \(url.absoluteString)")
accessQueue.sync {
let request = URLRequest(url)
var operation: ImageDownload
if let activeOperation = self.unsafeActiveOperation(with: url) {
operation = activeOperation
activeOperation.addCompletion(completion)
} else {
operation = self.operationType.init(request: request, in: self.urlSession)
let operation = self.operationType.init(request: request, in: self.urlSession)
operation.addCompletion(completion)
self.operations[url] = operation
if let operation = operation as? Operation {
print("[imagedownloader] added operation for url: \(url.absoluteString)")
self.downloadQueue.addOperation(operation)
}
}
if let completion = completion {
operation.addCompletion(completion)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ final class ReentrantImageDownloaderSpy: ReentrantImageDownloader {
var returnedDownloadResults = [URL: Data]()
var returnedOperation: ImageDownload?

func download(with url: URL, completion: CachedResponseCompletionHandler?) -> Void {
func download(with url: URL, completion: @escaping CachedResponseCompletionHandler) -> Void {
passedDownloadUrl = url
let response = cachedResponse(with: returnedDownloadResults[url], url: url)
completion?(response, response == nil ? DirectionsError.noData : nil)
completion(response, response == nil ? DirectionsError.noData : nil)
}

func activeOperation(with url: URL) -> ImageDownload? {
Expand Down
4 changes: 2 additions & 2 deletions Tests/MapboxNavigationTests/ImageDownloaderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class ImageDownloaderTests: TestCase {

func testDownloadWithImmidiateCancel() {
let incorrectUrl = URL(fileURLWithPath: "/incorrect_url")
downloader.download(with: incorrectUrl, completion: nil)
downloader.download(with: incorrectUrl, completion: {_,_ in })
let operation = (downloader.activeOperation(with: incorrectUrl) as! ImageDownloadOperation)
operation.cancel()

Expand All @@ -181,7 +181,7 @@ class ImageDownloaderTests: TestCase {

func testDownloadWithImmidiateCancelFromAnotherThread() {
let incorrectUrl = URL(fileURLWithPath: "/incorrect_url")
downloader.download(with: incorrectUrl, completion: nil)
downloader.download(with: incorrectUrl, completion: {_,_ in })
let operation = (downloader.activeOperation(with: incorrectUrl) as! ImageDownloadOperation)
DispatchQueue.global().sync {
operation.cancel()
Expand Down

0 comments on commit 7caca3a

Please sign in to comment.