Skip to content

Commit

Permalink
Merge pull request onevcat#790 from onevcat/fix/thread-racing
Browse files Browse the repository at this point in the history
Resolve a reading racing condition
  • Loading branch information
onevcat authored Oct 17, 2017
2 parents fb05558 + 091c3ce commit 32304cc
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions Sources/ImageDownloader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -412,13 +412,6 @@ extension ImageDownloader {
}
}
}

func clean(for url: URL) {
barrierQueue.sync(flags: .barrier) {
fetchLoads.removeValue(forKey: url)
return
}
}
}

// MARK: - NSURLSessionDataDelegate
Expand Down Expand Up @@ -510,11 +503,12 @@ class ImageDownloaderSessionHandler: NSObject, URLSessionDataDelegate, Authentic
guard let downloader = downloadHolder else {
return
}

downloader.clean(for: url)

if downloader.fetchLoads.isEmpty {
downloadHolder = nil

downloader.barrierQueue.sync(flags: .barrier) {
downloader.fetchLoads.removeValue(forKey: url)
if downloader.fetchLoads.isEmpty {
downloadHolder = nil
}
}
}

Expand Down

0 comments on commit 32304cc

Please sign in to comment.