Completion handler for UINavigationController pushViewControlleranimated?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
UINavigationController.pushViewController(_:animated:) does not provide a built-in completion handler. This is a frequent need when you must run code only after the navigation transition finishes.
This article shows safe patterns to simulate completion behavior using transition coordinator and CATransaction.
Core Sections
1) Transition coordinator approach
This hooks into UIKit transition lifecycle.
2) CATransaction fallback pattern
Useful in many cases, but transition coordinator is often semantically cleaner.
3) Handle non-animated pushes
Always invoke completion immediately for non-animated transitions to keep call-site behavior consistent.
4) Avoid retain cycles
When closure captures self, use weak capture where appropriate.
5) Prefer navigation delegation for global flow
For app-wide flow orchestration, consider navigation controller delegate methods instead of ad hoc completion wrappers.
6) Production checklist for iOS navigation completion flow
Code examples are necessary, but production readiness depends on how this pattern behaves under failure, load, and operational drift. Before rollout, define success criteria that are measurable. A useful baseline is three metrics: correctness (for example, expected output match rate), reliability (error rate and retry behavior), and latency (p95 or p99 execution time). Capture these metrics in a repeatable test environment rather than relying on ad hoc local runs. If external systems are involved, include at least one synthetic fault scenario such as timeout, malformed payload, or temporary dependency outage. This confirms the implementation fails predictably and recovers in a controlled way.
Document environment assumptions close to the code. Include runtime version constraints, required environment variables, and exact dependency versions used during validation. Many regressions come from mismatched environments rather than algorithmic changes. A short README snippet or inline comment that names these assumptions can prevent repeated troubleshooting later. Also define ownership for operational issues: who receives alerts, what threshold triggers action, and what rollback path is acceptable. Without explicit ownership and rollback criteria, otherwise small incidents can take longer to resolve.
A practical rollout sequence is:
- Run automated checks (lint, unit tests, static validation) in CI.
- Execute a smoke test against representative input sizes.
- Validate one failure mode and verify error visibility in logs.
- Deploy behind a feature flag or phased rollout if possible.
- Monitor key metrics for a defined stabilization window.
Finally, keep a short limitations section. State what the current approach intentionally does not optimize or support. This prevents accidental misuse by future contributors and keeps design discussions grounded in explicit tradeoffs. For long-lived systems, schedule periodic review of this implementation, especially after runtime upgrades or library changes. A lightweight maintenance cadence often catches compatibility issues before they become production incidents.
Common Pitfalls
- Assuming
pushViewControllerhas built-in completion support. - Forgetting to call completion when
animatedis false. - Capturing strong references in completion blocks and leaking controllers.
- Triggering follow-up UI actions before transition fully completes.
- Using multiple competing completion mechanisms in one navigation flow.
Summary
To run code after a push transition, wrap navigation in a helper using transition coordinator (or CATransaction fallback) and enforce consistent completion semantics for animated and non-animated paths. This prevents timing bugs in navigation-heavy iOS screens.
Related reading
- Computed read-only property vs function in Swift
- Concurrent vs serial queues in GCD
- Concurrent vs serial queues in GCD
- Conditional Binding if let error – Initializer for conditional binding must have Optional type
- Conditional Binding if let error – Initializer for conditional binding must have Optional type
- Conditionally use view in SwiftUI
- Configuration on demand is not supported by the current version of the Android Gradle plugin
- Connect Android to Arduino through GCM
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.