Unexpected SwiftUI Transaction Behavior
This minimal example demonstrates an unexpected behavior in SwiftUI's Transaction API:
var transaction = Transaction(animation: .none)
transaction.addAnimationCompletion { print("This should not be called!") }
The Issue
The completion handler is called immediately after creation, even though the transaction was never used in any SwiftUI animation context (like withTransaction
or other animation-related APIs).
Expected vs Actual Behavior
- Expected: The completion handler should only be called after the transaction is actually used in a SwiftUI animation.
- Actual: The completion handler is called right after creation, regardless of whether the transaction is used or not.
Current Workaround
To avoid this, I'm forced to implement defensive programming: only creating transactions with completion handlers at the exact moment they're going to be used. This adds unnecessary complexity and goes against the intuitive usage of the Transactions API.