Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# async await in Swift

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

like image 967
nor0x Avatar asked Dec 01 '16 11:12

nor0x


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...


2 Answers

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")
}
like image 167
Ahmad F Avatar answered Oct 20 '22 12:10

Ahmad F


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

like image 29
lee Avatar answered Oct 20 '22 10:10

lee