Skip to content

Commit

Permalink
Thread sanitization, GCD best practices
Browse files Browse the repository at this point in the history
  • Loading branch information
hallee committed Mar 21, 2021
1 parent ff3c0f1 commit 3c85ebd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
10 changes: 8 additions & 2 deletions Sources/RemoteImageDimensions/RemoteImageDataDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ import FoundationNetworking

final class RemoteImageDataDelegate: NSObject, URLSessionDataDelegate {

private let queue = DispatchQueue(label: "RemoteImageDataDelegateQueue", qos: .utility)
private let queue = DispatchQueue(label: "RemoteImageDataDelegateQueue", target: DispatchQueue.global())

var taskDelegates = [ImageDimensionTaskDelegate]()
private var taskDelegates: [ImageDimensionTaskDelegate] = []

func addTaskDelegate(_ delegate: ImageDimensionTaskDelegate) {
queue.async {
self.taskDelegates.append(delegate)
}
}

func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
queue.async {
Expand Down
2 changes: 1 addition & 1 deletion Sources/RemoteImageDimensions/RemoteImageDimensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public enum RemoteImage {
) -> RemoteImageDimensionsTask {
let dataTask = urlSession.dataTask(with: URLRequest.request(for: image, with: configuration))
let taskDelegate = ImageDimensionTaskDelegate(task: dataTask, completion)
delegate.taskDelegates.append(taskDelegate)
delegate.addTaskDelegate(taskDelegate)
dataTask.resume()
return RemoteImageDimensionsTask(cancel: { [weak taskDelegate, weak delegate] in
taskDelegate?.cancel()
Expand Down

0 comments on commit 3c85ebd

Please sign in to comment.