gpt4 book ai didi

swift - 如何在swift中同步循环

转载 作者:行者123 更新时间:2023-11-28 07:29:27 31 4
gpt4 key购买 nike

我正在尝试编写一个用于对列表进行排序的算法,并且我对 google maps api 使用网络调用(API 请求)来获取有关列表中两点之间距离的信息。

我正在使用 while 循环,并遍历列表,直到列表的大小为 0。

在每次迭代中,我都会进行网络调用,在它响应后,我会从列表中删除一些内容。

我已经尝试在下面的代码中使用信号量,但它没有按预期工作。

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()
}

打印语句应打印 6,5,4,3,2,1。

最佳答案

下面是一些简化的 playground 代码,演示了如何使用信号量来确保您的请求串行执行:

import UIKit
import PlaygroundSupport

PlaygroundPage.current.needsIndefiniteExecution = true

class SomeAsyncClass {

var unvistedPoints = [ 6,5,4,3,2,1 ]
let dispatchQueue = DispatchQueue(label: "taskQueue") // serial queue
let semaphore = DispatchSemaphore(value: 1)

public func doAsyncStuff() {
for point in self.unvistedPoints {
print("Queuing point \(point)")
dispatchQueue.async {
// block before sending the network request
self.semaphore.wait()
self.makeFakeNetworkRequest(point, completion: {
// request complete
print("Completed \(point)")
self.semaphore.signal()
})
}
}
}

func makeFakeNetworkRequest(_ point:Int, completion:()->()) {
let interval = TimeInterval(exactly: (arc4random() % 3) + 1)!
print("Point \(point): Sleeping for: \(interval)")
Thread.sleep(forTimeInterval: interval)
print("Point \(point): Awoken after: \(interval)")
completion()
}
}

var c = SomeAsyncClass()
c.doAsyncStuff()

这是输出:

Queuing point 6
Queuing point 5
Queuing point 4
Point 6: Sleeping for: 3.0
Queuing point 3
Queuing point 2
Queuing point 1
Point 6: Awoken after: 3.0
Completed 6
Point 5: Sleeping for: 3.0
Point 5: Awoken after: 3.0
Completed 5
Point 4: Sleeping for: 3.0
Point 4: Awoken after: 3.0
Completed 4
Point 3: Sleeping for: 3.0
Point 3: Awoken after: 3.0
Completed 3
Point 2: Sleeping for: 3.0
Point 2: Awoken after: 3.0
Completed 2
Point 1: Sleeping for: 3.0
Point 1: Awoken after: 3.0
Completed 1

话虽如此,这并不是最好的方法。您最好使用为此目的而设计的 iOS 结构,即 OperationQueue——它具有精细的并发控制 (maxConcurrentOperationCount),并且可以用作 URLSession (delegateQueue)。如果适合您的需要,我建议您使用该构造。

关于swift - 如何在swift中同步循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55342669/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com