diff --git a/MapboxSceneKit/MapboxImageAPI.swift b/MapboxSceneKit/MapboxImageAPI.swift index bb595d2..b672ab6 100644 --- a/MapboxSceneKit/MapboxImageAPI.swift +++ b/MapboxSceneKit/MapboxImageAPI.swift @@ -113,7 +113,10 @@ public final class MapboxImageAPI: NSObject { let group = DispatchGroup() let groupID = UUID() - pendingFetches[groupID] = [UUID]() + self.pendingFetchesDispatchQueue.sync(flags: .barrier) { [weak self] in + guard let self = self else { return } + self.pendingFetches[groupID] = [UUID]() + } var completed: Int = 0 let total = bounding.xs.count * bounding.ys.count @@ -196,7 +199,10 @@ public final class MapboxImageAPI: NSObject { let group = DispatchGroup() let groupID = UUID() - pendingFetches[groupID] = [UUID]() + pendingFetchesDispatchQueue.sync(flags: .barrier) { [weak self] in + guard let self = self else { return } + self.pendingFetches[groupID] = [UUID]() + } var completed: Int = 0 let total = bounding.xs.count * bounding.ys.count @@ -236,7 +242,10 @@ public final class MapboxImageAPI: NSObject { } group.notify(queue: DispatchQueue.main) { - self.pendingFetches.removeValue(forKey: groupID) + self.pendingFetchesDispatchQueue.sync(flags: .barrier) { [weak self] in + guard let self = self else { return } + self.pendingFetches.removeValue(forKey: groupID) + } completion(error == nil ? imageBuilder.makeImage() : nil, error?.toNSError()) } @@ -248,13 +257,16 @@ public final class MapboxImageAPI: NSObject { **/ @objc func cancelRequestWithID(_ groupID: UUID) { - guard let tasks = pendingFetches[groupID] else { - return - } - for task in tasks { - httpAPI.cancelRequestWithID(task) + self.pendingFetchesDispatchQueue.sync(flags: .barrier) { [weak self] in + guard let tasks = pendingFetches[groupID] else { + return + } + for task in tasks { + httpAPI.cancelRequestWithID(task) + } + + self?.pendingFetches.removeValue(forKey: groupID) } - pendingFetches.removeValue(forKey: groupID) } //MARK: - Helpers diff --git a/MapboxSceneKit/Tile Fetching/MapboxHTTPAPI.swift b/MapboxSceneKit/Tile Fetching/MapboxHTTPAPI.swift index a4882c5..e46d528 100644 --- a/MapboxSceneKit/Tile Fetching/MapboxHTTPAPI.swift +++ b/MapboxSceneKit/Tile Fetching/MapboxHTTPAPI.swift @@ -26,9 +26,10 @@ enum FetchError: Int { } internal final class MapboxHTTPAPI { + private static var tileDownloadTaskDispatchQueue = DispatchQueue(label: "com.mapbox.scenekit.api", attributes: [.concurrent]) private static var operationQueue: OperationQueue = { var operationQueue = OperationQueue() - operationQueue.underlyingQueue = DispatchQueue(label: "com.mapbox.scenekit.api", attributes: [.concurrent]) + operationQueue.underlyingQueue = tileDownloadTaskDispatchQueue operationQueue.name = "Mapbox API Queue" operationQueue.maxConcurrentOperationCount = 10 return operationQueue