Swift share variable between threads4/1/2023 If you call await within an asynchronous function, it creates a suspension point that may switch execution to any pending code, even to the same function if it was called multiple times. It could be dispatched on any thread (unless it is started from the Main Actor). If you run a code from Task, you can’t make any assumptions about the thread.If you add an async keyword to your function, it can be called on a background thread even if you start the function from the Main Thread.This is just the way Swift Concurrency works, but it may cause problems if you don’t know what’s going on under the hood. Maybe “a problem” is not the best word for that. To continue you must first understand the basic problems of async-await. Although, I will try to share with you some of my observations hopefully making you more aware. To be honest, even after performing tests, reading documentation, and watching WWDC I’m still not fully confident with Swift Concurrency. Unfortunately, there are many traps around concurrency, and no… the compiler doesn’t check everything. This way of “selling” Swift Concurrency encourages people to just jump in, add async-await to an existing code, and run some Tasks not really knowing what is going on under the hood. Basically, Swift Concurrency is advertised as safe to use, because in theory the correctness is being checked by the compiler. If you learn from tutorials or even from the documentation, it’s really hard to find some details on how it works under the hood. However, with great power comes great responsibility. Support for async-await has been to me the most awaited feature in Swift. Swift Concurrency provides a really nice way of writing asynchronous code.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |