27.4 · Actor reentrancy, task ordering, SwiftUI identity, and how to answer
The newer puzzles share one theme: what can happen at a suspension point, and what SwiftUI thinks a view is. Both are about identity over time.
Mental model. At every await the world can change; and in SwiftUI, state belongs to identity, not to a position on screen.
Concurrency puzzles
Reentrancy on an actor and the order in which unstructured tasks run.
- Two tasks each withdraw 80 from an actor account holding 100, with an await between the balance check and the update. What is the balance, and how do you fix it?
An actor runs one piece of its code at a time, but a piece ends at every await, and other calls can run while this one is suspended (reentrancy, Q295). In… - In what order do these prints appear when a main-actor function creates a Task, yields, and starts a detached task?
Task { … } started from the main actor inherits the main actor’s isolation, so its body must run on the main actor, and the main actor is busy running demo()…
SwiftUI and method
Row identity after a deletion, and a routine for any “what does this print?”.
- Rows keep a @State value. After removing the first of three items, which state does each remaining row show when the list is driven by indices, and by ids?
SwiftUI stores @State against a view’s identity, and in a ForEach the identity is the value of the id key path (Q260). With ForEach(items.indices, id: \.self)… - What is a reliable routine for answering “what does this print?” under interview pressure?
A puzzle is a short program with one hidden rule, and the routine is to find that rule before you start tracing. Read once for what the code is about (a…