7

I'm currently playing with closures and chained completions in 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.

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?

Thank you

3 Answers 3

2

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:

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")
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer - is it possible to access parameters in different "queue items"?
2

Currently concurrency in Swift language is not there yet. It's still a proposal for Swift 5 on Swift evolution and here is link of it: concurrency proposal. 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. I am still looking for concurrency in Swift language

Comments

0

You can use this framework for Swift coroutines - 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)
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.