Skip to content
Open
Show file tree
Hide file tree
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
30 changes: 21 additions & 9 deletions MapboxSceneKit/MapboxImageAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Copy link

@psahgal psahgal Apr 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not too familiar with how sync() and group.notify() work, but is it possible this could cause some sort of deadlock on the main queue? From the docs on DispatchQueue:

Attempting to synchronously execute a work item on the main queue results in deadlock.

(I used a slightly different approach for wrapping this call in #80.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure either. I'll take a look at your change. It might be the way to go.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I closed the pull request already, but feel free to reopen it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot speak into the possibility of deadlock here but this is working great for me

completion(error == nil ? imageBuilder.makeImage() : nil, error?.toNSError())
}

Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion MapboxSceneKit/Tile Fetching/MapboxHTTPAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down