SwiftUI Transaction completion handler is being called unexpectedly

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.

So we can understand the issue better, I'd appreciate it if you could open a feedback report, include a test project that reproduces the issue and post the FB number here once you do. Bug Reporting: How and Why? has tips on creating your bug report.

SwiftUI Transaction completion handler is being called unexpectedly
 
 
Q