gpt4 book ai didi

ios - 如何一个接一个地执行多个异步请求

转载 作者:搜寻专家 更新时间:2023-10-31 19:33:26 24 4
gpt4 key购买 nike

我有一组异步执行的请求。但是,每个下一个请求只能在上一个请求完成时开始(由于数据依赖性)。

由于所有请求都应该以正确的顺序完成,DispatchGroup() 似乎没有用。

我目前实现了 DispatchSemaphore(),但我觉得这不是最佳解决方案,因为我想确保所有请求都在后台执行。

let semaphore = DispatchSemaphore(value: requests.count)

for request in requests {
apiManager().performAsyncRequest(request, failure: { error in
print(error); semaphore.signal()
}) { print(“request finished successful”)
// Next request should be performed now
semaphore.signal()
}
}
semaphore.wait()

有没有更好的方法来执行此操作?

注意:根据以下答案之一的实现,我遇到了 apiManager() 不是线程安全的(由于使用了 Realm 数据库)。

为了明确这个问题并以线程安全的方式考虑答案,使用线程安全的 performAsyncRequest 定义:

public func performAsyncRequest(_ requestNumber: Int, success: @escaping (Int) -> Void)->Void {
DispatchQueue(label: "performRequest").async {
usleep(useconds_t(1000-requestNumber*200))
print("Request #\(requestNumber) starts")
success(requestNumber)
}
}

使用 DispatchSemaphore 的解决方案

        let semaphore = DispatchSemaphore(value: 1)
DispatchQueue(label: "requests").async {
for requestNumber in 0..<4 {
semaphore.wait()
performAsyncRequest(requestNumber) { requestNumber in
print("Request #\(requestNumber) finished")
semaphore.signal()
}
}
}

预期输出:

Request #0 starts
Request #0 finished
Request #1 starts
Request #1 finished
Request #2 starts
Request #2 finished
Request #3 starts
Request #3 finished

操作尝试失败

    var operations = [Operation]()

for requestNumber in 0..<4 {
let operation = BlockOperation(block: {
performAsyncRequest(requestNumber) { requestNumber in
DispatchQueue.main.sync {
print("Request #\(requestNumber) finished")
}
}
})

if operations.count > 0 {
operation.addDependency(operations.last!)
}
operations.append(operation)
}

let operationQueue = OperationQueue.main
operationQueue.addOperations(operations, waitUntilFinished: false)

输出不正确:

Request #0 starts
Request #1 starts
Request #2 starts
Request #3 starts
Request #0 finished
Request #3 finished
Request #2 finished
Request #1 finished

我的感觉是,使用 Operation 也应该可以实现这一点,但我不知道它是否会比使用 DispatchSemaphore 更好。

最佳答案

您在 DispatchSemaphore 的正确轨道上确保在前一个调用完成之前不会启动异步调用。我只是确保管理异步 API 调用的代码在后台运行:

let backgroundQueue = DispatchQueue(label: "requests")
let semaphore = DispatchSemaphore(value: 1)

backgroundQueue.async {
var requestNumber = 1

for request in requests {
semaphore.wait()

let currentRequestNumber = requestNumber

print("Request launched #\(requestNumber)")

apiManager().performAsyncRequest(request,
failure: {
error in
print("Request error #\(currentRequestNumber)")
semaphore.signal()
}) {
print("Request result #\(currentRequestNumber)")
semaphore.signal()
}

requestNumber = requestNumber + 1
}
}

当 for 循环在后台循环中运行并在等待前一个请求完成后启动每个请求时,代码将立即继续执行。

或者如果 apiManager() 不是线程安全的:

let semaphore = DispatchSemaphore(value: 1)

var requestNumber = 1

for request in requests {
semaphore.wait()

let currentRequestNumber = requestNumber

print("Request launched #\(requestNumber)")

apiManager().performAsyncRequest(request,
failure: {
error in
print("Request error #\(currentRequestNumber)")
semaphore.signal()
}) {
print("Request result #\(currentRequestNumber)")
semaphore.signal()
}

requestNumber = requestNumber + 1
}

这有一个限制,即 for 循环将一直执行到最后一个请求开始执行。但是,如果您调用的代码不是线程安全的,则没有办法解决。

关于ios - 如何一个接一个地执行多个异步请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39871353/

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