Restore Navigation Only After The Data Is Ready

The app saves a navigation path correctly. On its next launch, restoration runs before the local database finishes opening. The destination lookup returns nil, so the app discards the route and saves an empty path over the old one.

Nothing was deleted. The app confused “not ready to answer” with “this destination does not exist.”

Our enum-router article covers representing navigation as values and connecting those values to a NavigationStack. Here, let’s make the restoration decision explicit.

Decode first, then validate

A decoded route is syntactically valid data. It does not prove that the account is still signed in, the project still exists, or the user still has access to the task.

Keep a version and account identity beside the route array. Then let the data owner answer three states instead of returning an ambiguous optional:

import Foundation

enum SavedRoute: Codable, Equatable {
    case project(UUID)
    case task(projectID: UUID, taskID: UUID)
}

struct SavedNavigation: Codable {
    let version: Int
    let accountID: UUID
    let routes: [SavedRoute]
}

enum RouteAvailability { case unresolved, available, unavailable }
enum RestorationPlan: Equatable {
    case wait
    case restore([SavedRoute])
}

func restorationPlan(
    for saved: SavedNavigation,
    accountID: UUID,
    availability: (SavedRoute) -> RouteAvailability
) -> RestorationPlan {
    guard saved.version == 1, saved.accountID == accountID else {
        return .restore([])
    }
    var prefix: [SavedRoute] = []
    for route in saved.routes {
        switch availability(route) {
        case .unresolved: return .wait
        case .unavailable: return .restore(prefix)
        case .available: prefix.append(route)
        }
    }
    return .restore(prefix)
}

unresolved means the app cannot make a reliable decision yet. unavailable means the authoritative data or authorization state says the route cannot be restored. A network error or an unopened database should not automatically become unavailable.

The function returns a plan and does not mutate the router. That keeps the decision independently testable and lets the scene control when to apply it.

Restore a valid prefix

Suppose the saved stack contains a project and then one of its tasks. The project still exists, but the task was deleted. Restoring the project is useful; pushing a broken task screen is not.

The planner stops at the first unavailable route. It does not filter invalid entries out of the middle and attach unrelated later destinations to a different parent. Your availability implementation must also validate parent-child relationships and access, not just find a matching task ID somewhere in the database.

An unresolved route makes the plan wait. Keep a separate loading or retry state for the scene so a failed data load does not become an endless blank screen. The user should still be able to navigate somewhere else while restoration is pending.

Give new user intent priority

A saved path is a convenience from a previous session. A deep link the user just opened, or a project they just selected, is a new instruction.

Keep a restoration token or a scene-owned “restoration still pending” flag. When a user action or deep link takes ownership of navigation, invalidate the pending restoration. A later database callback must not replace the path with yesterday’s state.

Apply the plan at most once for that restoration attempt. Normal refreshes can change the data snapshot many times. Reapplying the saved path on every refresh would make Back appear broken because the next update pushes the destination again.

Do not persist the placeholder path

While the app is waiting, the visible router may temporarily contain an empty path. That does not mean the user chose to return to the root.

Keep the saved candidate separate from the visible path until restoration is resolved or superseded by user intent. Gate your persistence observer so initial setup cannot erase the candidate before validation finishes. Once the user navigates, their current path becomes the value to preserve.

This is also why each scene needs its own restoration state. Two windows can legitimately show different projects under the same account. An app-wide singleton path turns one window’s startup into another window’s navigation change.

Test the state transitions

Start with a two-route saved stack. Verify unresolved data produces wait, a missing child produces the parent prefix, and an available child restores the whole path. An account mismatch should not even call the destination resolver.

Then test the scene integration: begin restoration, accept a deep link, finish the old data load, and confirm the deep link still owns the path. Also verify that the first empty router value did not overwrite stored navigation.

September 2026 review: the Swift 6.4 planner fixture passed unresolved-data waiting, prefix restoration, full restoration, account-boundary rejection, and Codable round-trip checks. Scene storage, deep-link precedence, and multiple-window integration still need app-level tests.

Restoration works best as a small negotiation between saved intent and current data. Let the data owner say when it knows enough, and let the user’s next action end the negotiation immediately.