简体   繁体   English

Swift 中的 C# 异步等待

[英]C# async await in Swift

I'm currently playing with closures and chained completions in Swift .我目前正在Swift使用闭包和链式完成。 I'm quite familiar with the C# style of async and await , so I was wondering how to "translate" the following snippet from C# to Swift .我非常熟悉asyncawaitC#风格,所以我想知道如何将以下代码段从C# “翻译”为Swift

public async Task SomeFunction(string inputString)
{
    var first = await GetFirstVariableAsync(inputString);
    var second = await GetSecondVariableAsync(first);

    if (second == "some condition")
    {
        first = await GetFirstVariableAsync(inputString);
        var second = await GetSecondVariableAsync(first);
    }
}

Does Swift has a similar construct like await , to wait for a function to complete, without having to nest multiple completion blocks? Swift是否有类似await构造,以等待函数完成,而不必嵌套多个完成块?

Thank you谢谢

If I'm not mistaking, what are you looking for is:如果我没记错的话,你要找的是:

Serial Dispatch Queues:串行调度队列:

Serial queues (also known as private dispatch queues) execute one task at a time in the order in which they are added to the queue.串行队列(也称为私有调度队列)按添加到队列的顺序一次执行一项任务。 The currently executing task runs on a distinct thread (which can vary from task to task) that is managed by the dispatch queue.当前正在执行的任务在由调度队列管理的不同线程上运行(可能因任务而异)。 Serial queues are often used to synchronize access to a specific resource.串行队列通常用于同步对特定资源的访问。 You can create as many serial queues as you need, and each queue operates concurrently with respect to all other queues.您可以根据需要创建任意数量的串行队列,并且每个队列相对于所有其他队列同时运行。 In other words, if you create four serial queues, each queue executes only one task at a time but up to four tasks could still execute concurrently, one from each queue.换句话说,如果您创建四个串行队列,则每个队列一次仅执行一项任务,但最多仍可以同时执行四个任务,每个队列一个。 For information on how to create serial queues, see Creating Serial Dispatch Queues.有关如何创建串行队列的信息,请参阅创建串行调度队列。

Swift 3:斯威夫特 3:

let serialQueue = DispatchQueue(label: "serialQueue")

serialQueue.sync {
    print("running first task")
}

serialQueue.sync {
    print("I will wait the first task and then I'll do my own task")
}

Currently concurrency in Swift language is not there yet.目前concurrency in Swift language还没有concurrency in Swift language It's still a proposal for Swift 5 on Swift evolution and here is link of it: concurrency proposal .它仍然是Swift 5关于Swift 进化的提案,这里是它的链接: 并发提案 But If this time you are developing apps using Cocoa / Cocoa touch then you can use Dispatch , but I think it's has ugly syntax.但是如果这次您使用Cocoa / Cocoa touch开发应用程序,那么您可以使用Dispatch ,但我认为它的语法很难看。 I am still looking for concurrency in Swift language我还在寻找Swift语言的并发

You can use this framework for Swift coroutines - https://github.com/belozierov/SwiftCoroutine您可以将此框架用于 Swift 协程 - https://github.com/belozierov/SwiftCoroutine

func awaitAPICall(_ url: URL) throws -> String? {
    let future = URLSession.shared.dataTaskFuture(for: url)
    let data = try future.await().data
    return String(data: data, encoding: .utf8)
}

func load(url: URL) {
    DispatchQueue.main.startCoroutine {
        let result1 = try self.awaitAPICall(url)
        let result2 = try self.awaitAPICall2(result1)
        let result3 = try self.awaitAPICall3(result2)
        print(result3)
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM