Swift 5.5 came out with a lot of updates. And we mean a lot. You can tell that Apple decided to change our experience with Swift drastically. Among many other updates discussed in our recent article, concurrent programming is the most prominent one.
Why is this feature so crucial for Swift, and how to use concurrent programming in Swift? Let’s discuss.
Simply call the async/await function
The easiest way to get hold of the concurrent, or as it is also known, asynchronous programming, is to call the function via async or await keyword. What happens when you use those? Each function goes in a separate thread, making it somewhat independent from the others instead of running one by one.
As a result, you can have bigger gaps between the blocks of code or run them simultaneously. It makes managing larger-scale projects way easier as your asynchronous code will run almost like a usual synchronous one.
Combine async/await function with the try/catch one
While it is possible to use the async/await function on its own, you can get the most out of it if combined with the try/catch. Such a combination allows your functions to throw errors. In such a way, you can always be sure that your code will be executed in the right way and no surprises will ever come up.
For instance, you might use the try await function. In this way, an error might come up when all of your asynchronous code has run.
Use async/await sequences
One more way to apply the async/await function more effectively is to use it as a sequence. In this way, you can decide to process values in a specific order but not precompute them in advance. Sequences give you even more control over how the code will run at any point.
Apply unstructured concurrency
Concurrency is quite a complex notion that comes in different shapes. For example, there is structured and unstructured concurrency.
It’s important to understand that not in every case will you have a structured pattern in your asynchronous tasks, or some of them won’t follow the rules. You still have to possess some control over them, so that’s when the unstructured concurrency approach can help you.
How does it work? Unstructured concurrency allows you to launch async tasks from non-async contexts or even completely detach some tasks and make them unstructured so that they are not dependent on the parent task and run on their own.
Don’t forget about Actors
Along with concurrency, Swift 5.5 also introduced Actors. We explained what those are more thoroughly here. A quick recap: actors are particular reference types, just like our regular classes. The difference is that actors are designed specifically for the concurrent environment. Needless to say that actors go perfectly well with the async and await functions.
The bottom line: why use concurrency
Concurrency is an addition to Swift that was long-awaited by many coders. And that’s for a reason: it allows you to suspend and resume your code, run multiple operations, or choose which pieces of code to carry out and which to stop at any given point. All of this gives your solution the flexibility that was never there before.
Top comments (0)