swift - 如何取消具有从 `async` 操作启动返回的可取消类型的 `async` 函数

标签 swift async-await cancellation

我需要支持取消一个函数,该函数返回一个可以在启动后取消的对象。在我的例子中,requester 类位于我无法修改的第 3 方库中。

actor MyActor {

    ...

    func doSomething() async throws -> ResultData {

        var requestHandle: Handle?
    
        return try await withTaskCancellationHandler {
            requestHandle?.cancel() // COMPILE ERROR: "Reference to captured var 'requestHandle' in concurrently-executing code"
        } operation: {

            return try await withCheckedThrowingContinuation{ continuation in
            
                requestHandle = requester.start() { result, error in
            
                    if let error = error
                        continuation.resume(throwing: error)
                    } else {
                        let myResultData = ResultData(result)
                        continuation.resume(returning: myResultData)
                    }
                }
            }
        }
    }

    ...
}

我已经查看了其他 SO 问题和此线程:https://forums.swift.org/t/how-to-use-withtaskcancellationhandler-properly/54341/4

有些情况非常相似,但又不完全相同。由于此错误,此代码无法编译:

"Reference to captured var 'requestHandle' in concurrently-executing code"

我假设编译器在初始化之前试图保护我不使用 requestHandle。但我不确定如何解决这个问题。 Swift 论坛讨论线程中显示的其他示例似乎都有一个模式,其中可以在调用其 start 函数之前初始化 requester 对象。

我还尝试将 requestHandle 保存为类变量,但我在同一位置遇到了不同的编译错误:

Actor-isolated property 'profileHandle' can not be referenced from a Sendable closure

最佳答案

你说:

I assume the compiler is trying to protect me from using the requestHandle before it’s initialized.

或者,更准确地说,它只是保护您免受种族歧视。您需要将您的交互与您的“请求者”和那个 Handle 同步。

But I’m not sure how else to work around this problem. The other examples shown in the Swift Forum discussion thread all seem to have a pattern where the requester object can be initialized before calling its start function.

是的,这正是您应该做的。不幸的是,您没有分享您的 requester 在哪里被初始化或者它是如何实现的,所以我们很难对您的具体情况发表评论。

但根本问题是您需要同步您的startcancel。因此,如果您的 requester 还没有这样做,您应该将其包装在一个提供线程安全交互的对象中。在 Swift 并发中做到这一点的标准方法是使用 actor。


例如,假设您正在包装一个网络请求。要与此同步您的访问,您可以创建一个 Actor :

actor ResponseDataRequest {
    private var handle: Handle?

    func start(completion: @Sendable @escaping (Data?, Error?) -> Void) {
        // start it and save handle for cancelation, e.g.,
        
        handle = requestor.start(...)
    }

    func cancel() {
        handle?.cancel()
    }
}

将网络请求的启动和取消包装在一个 actor 中。然后你可以做这样的事情:

func doSomething() async throws -> ResultData {
    let responseDataRequest = ResponseDataRequest()

    return try await withTaskCancellationHandler {
        Task { await responseDataRequest.cancel() }
    } operation: {
        return try await withCheckedThrowingContinuation { continuation in
            Task {
                await responseDataRequest.start { result, error in
                    if let error = error {
                        continuation.resume(throwing: error)
                    } else {
                        let resultData = ResultData(result)
                        continuation.resume(returning: resultData)
                    }
                }
            }
        }
    }
}

当您确认一切都在您检查的延续上工作时,您显然可以转向不安全的延续。

关于swift - 如何取消具有从 `async` 操作启动返回的可取消类型的 `async` 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71898080/

相关文章:

swift - 升级到 Xcode 11 后,Bundle 资源的 URL 不再起作用

ios - 导航栏中的 Facebook 分享按钮显示为非事件状态

ios - 上传图像解析ios 9

node.js - aws-amplify auth currentSession 不返回当前用户

swift - 数据库值计数

c# - 异步/等待异常处理

c# - 是否可以使对象构造异步?

c# - 取消长操作

c# - 如何暂停 BackgroundWorker?或者类似的东西

swift - 如何在 GCD 中停止 DispatchWorkItem?