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

fix for taskMap dictionary crasher. Issue #305 #308

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
24 changes: 20 additions & 4 deletions Source/Operation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,9 @@ public class DelegateManager: NSObject, URLSessionDataDelegate, URLSessionDownlo
/// this is for global request handling
var requestHandler:((inout URLRequest) -> Void)?

/// This is for handling unsafe thread operations on taskMap
private let concurrentTaskMapQueue = DispatchQueue(label: "TaskMapThreadSafeQueue", attributes: .concurrent)

var taskMap = Dictionary<Int,Response>()
//"install" a task by adding the task to the map and setting the completion handler
func addTask(_ task: URLSessionTask, completionHandler:@escaping ((Response) -> Void)) {
Expand All @@ -357,18 +360,31 @@ public class DelegateManager: NSObject, URLSessionDataDelegate, URLSessionDownlo

//"remove" a task by removing the task from the map
func removeTask(_ task: URLSessionTask) {
taskMap.removeValue(forKey: task.taskIdentifier)
concurrentTaskMapQueue.async(flags: .barrier) { [weak self] in
self?.taskMap.removeValue(forKey: task.taskIdentifier)
}
// MARK: removeTask(_:) stays in stack until
// responseForTask(_:) returns
let _ = responseForTask(task)
}

//add the response task
func addResponseForTask(_ task: URLSessionTask) {
if taskMap[task.taskIdentifier] == nil {
taskMap[task.taskIdentifier] = Response()
concurrentTaskMapQueue.sync {
if taskMap[task.taskIdentifier] == nil {
concurrentTaskMapQueue.async(flags: .barrier) { [weak self] in
self?.taskMap[task.taskIdentifier] = Response()
}
}
}
}
//get the response object for the task
func responseForTask(_ task: URLSessionTask) -> Response? {
return taskMap[task.taskIdentifier]
var response: Response?
concurrentTaskMapQueue.sync {
response = self.taskMap[task.taskIdentifier]
}
return response
}

//handle getting data
Expand Down