What is the key feature of structured concurrency that ensures the following code works correctly?
iOS interview question for Intermediate practice.
Answer
The await keyword guarantees that manager.makePurchase() completes fully before manager.updateUI() is called, preventing a race condition.
Explanation
This code is safe and works as intended. The await keyword in await manager.makePurchase() suspends the Task and only resumes execution on the next line once the makePurchase async function has completely finished (including the Task.sleep and the property update). This sequential execution within the Task ensures that updateUI() is only called after purchaseCompleted has been set to true, thus preventing any race conditions.