atlantis icon indicating copy to clipboard operation
atlantis copied to clipboard

DispatchGroup wait blocks Atlantis delegate

Open Sherlouk opened this issue 4 years ago • 1 comments

Hey there 👋

Weird 'bug' (is it a bug? maybe not...) that lost me a good hour this evening 😅

If you have a DispatchGroup and are using the sync group.wait() function then Atlantis's delegate call is not triggered until after the group has finished.

Simplified example, I'm assuming you've already setup Atlantis with a print("Event Received") in the delegate call

let group = DispatchGroup()

let task = URLSession.shared.dataTask(with: URL(string: url)!) { _, _, _ in
  print("Network Complete")
  group.leave()
}

group.enter()
print("Starting")
task.resume()

_ = group.wait(timeout: .now() + 60)
print("Finished")

In this example you will see "Starting" "Network Complete" "Finished" "Event Received".

If you switch from .wait() to .notify() like:

group.notify(queue: .main) {
  print("Finished")
}

Then this works and you get the expected: "Starting", "Event Received", "Network Complete", "Finished".

Essentially with wait Atlantis doesn't trigger the delegate call until the entire group is finished.

Sherlouk avatar Feb 23 '21 20:02 Sherlouk

Hey @Sherlouk,

Since the delegate is called from the main thread, so if you're using .wait(), it will block the main thread. Therefore, Atlantis's delegate could not be trigger until the Group is finished.

Maybe I can introduce the delegate queue, that you can pass the queue you would receive the delegate. It might fix your problem 😄

NghiaTranUIT avatar Feb 23 '21 23:02 NghiaTranUIT