简体   繁体   English

Swift:我可以将异步/等待语法与已经同步返回值的 function 一起使用吗?

[英]Swift: can I use async/await syntax with a function that already returns synchronously a value?

I know that I can use async/await to replace this:我知道我可以使用 async/await 来替换它:

func test(_ completion: @escaping (Int) -> Void) {
    // ...
    completion(foundValue)
}

with this:有了这个:

func test async -> Int {
    let result = await calculate()
    return result
}

However, can I do something if the initial function is like this?但是,如果最初的 function 是这样的,我可以做些什么吗?

func test(_ completion: @escaping (Int) -> Void) -> Int {
    // ...
}

Thank you for your help谢谢您的帮助

If your function doesn't need to wait for completion handler call then you can create a top level task and call your completion handler from the task after doing asynchronous work:如果您的 function 不需要等待完成处理程序调用,那么您可以创建一个顶级任务并在执行异步工作后从任务中调用您的完成处理程序:

func test(_ completion: @escaping (Int) -> Void) -> Int {
    // synhronous calls
    Task {
        // async funtion calls
        completion(value)
    }
    return value
}

If you need to wait for all your asynchronous task completion before returning, you can use a semaphore for that:如果您需要在返回之前等待所有异步任务完成,您可以使用信号量:

func test(_ completion: @escaping (Int) -> Void) -> Int {
    let semaphore = DispatchSemaphore(value: 0)
    // synhronous calls
    Task {
        // async funtion calls
        completion(value)
        // signal async task completion
        semaphore.signal()
    }
    // wait for async task completion
    semaphore.wait()
    return value
}

Note that using semaphore.wait() blocks the current thread and blocking the thread can cause undefined runtime behaviors in modern concurrency.请注意,使用semaphore.wait()会阻塞当前线程,并且阻塞线程会导致现代并发中未定义的运行时行为。 So proceed with caution when using such approach.因此,在使用这种方法时要谨慎行事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS Swift如何在需要返回值的函数中等待异步任务 - iOS swift how can I await an async task inside a function that needs a return value 如何在 Swift 5.5 中将 async/await 与 SwiftUI 一起使用? - How can I use async/await with SwiftUI in Swift 5.5? 我可以使用 Swift async/await 连续调用 void 方法吗? - Can I use Swift async/await to call void methods in succession? 如何在 swift 中使用 async / await? - How do I use async / await in swift? Swift await/async - 如何同步等待异步任务完成? - Swift await/async - how to wait synchronously for an async task to complete? 您可以在 swift 中使用 async/await 以及测试调度程序吗? - Can you use async/await in swift along with a test scheduler? 我们可以定义一个 async/await function 来立即返回一个值,以及异步返回另一个值(正如您通常所期望的那样)吗? - Can we define an async/await function that returns one value instantly, as well as another value asynchronously (as you'd normally expect)? 如何从 GCD(DispatchQueue)转换为 Swift 异步/等待? - How can I convert to Swift async/await from GCD (DispatchQueue)? Swift 编写一个带有返回值的 async/await 方法 - Swift write an async/await method with return value 在 Swift 5.5 使用 async/await 中重复出现 function - Recurring function in Swift 5.5 using async/await
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM