Im trying to write an algorithm for sorting a list, and I use a network call (API request) to the google maps api for information about the distance between two points in a list.
I am using a while loop, and iterating over the list, until the size of the list is 0.
On each iteration, I make a network call, and after it responds, I remove something from the list.
I've tried using semaphores with the code below, and it does not work as expected.
let semaphore = DispatchSemaphore(value: 1)
let dispatchQueue = DispatchQueue(label: "taskQueue")
dispatchQueue.async {
while unvistedPoints.count > 0{
print("The size of the list is ", unvisited.count)
self.findNextVistablePoint(visited: visitedPoints, unvisted: unvistedPoints, completion: { (pointToVisit) in
let indexofPointToVisit = unvistedPoints.firstIndex(where: {$0 === pointToVisit})
unvistedPoints.remove(at: indexofPointToVisit!)
visitedPoints.append(pointToVisit)
semaphore.signal()
})
semaphore.wait()
}
The print statement should print 6,5,4,3,2,1.