NavigationStack: pushed view renders blank when syncState's delayed effect runs after a push

Open
#572 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 4 days

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
Half a day
Newbie friendliness
55/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
kotlin, swift
Domain
frontend, mobile

Research direction

Start in Navigator.syncState() in Sources/SkipUI/SkipUI/Containers/Navigation.swift, where the delayed LaunchedEffect builds backStackState from stackKeys captured when the effect started. Reproduce a push shortly after a heavy first composition on Android, then check whether the proposed change (iterating navBackStack.toList() when the delay ends) keeps the pushed entry's state. Done when a pushed view renders reliably after cold start and Back pops normally.

Written by the indexing model from the issue text.

Description

Summary

A view pushed onto a NavigationStack can render blank (no content, no top bar) when the push happens while the main thread is busy shortly after the stack was first composed. The entry stays on the back stack; pressing Back re-presents the destination.

Cause

Navigator.syncState() (Sources/SkipUI/SkipUI/Containers/Navigation.swift, unchanged on main and in 1.63.0) prunes backStackState in a delayed effect:

let stackKeys = navBackStack.toList()
...
LaunchedEffect(stackEffectKey) {
    delay(1000) // 1 second
    var syncedBackStackState: [String: BackStackState] = [:]
    for key in stackKeys {            // <- keys captured when the effect STARTED
        ...
    }
    backStackState = syncedBackStackState
}

If the main thread is blocked (e.g. a heavy first composition of the root view, or a cold JIT right after app start), the effect for the old stack state ([root]) can resume after a new entry has been pushed but before recomposition cancels it (its key changed). It then rebuilds backStackState from the stale keys and drops the state of the entry that was just pushed. entry<SkipNavigationStackPushKey> finds no state and returns without rendering anything:

guard let state = navigator.value.state(forPushKey: navKey), let targetValue = state.targetValue else {
    return
}
Evidence

Logging added to both places (Android emulator, API 36, SkipUI 1.62.0, Skip Fuse app with a ~600-row list as the stack root, tap ~2 s after first showing the tab, ~12 s after cold start):

W BUG008: syncState drops 1, effect stack: navigationroot, now: [navigationroot, 99/0]
W BUG008: entry without state: skip.ui.SkipNavigationStackPushKey@7e4

8 of 8 cold starts reproduced it. It happens for both navigationDestination(isPresented:) and navigationDestination(item:), with NavigationLink as well, and also with navigationStackTransitions set to .none. Lighter stacks (where the root composes quickly) were not affected. Slower devices widen the window.

Proposed fix

Sync against the current back stack when the delayed effect runs:

-            for key in stackKeys {
+            for key in navBackStack.toList() {

With this change: 0 of 8 cold starts blank (previously 8 of 8), Back pops normally. We ship it from a fork for now: https://github.com/woxmaster-cell/skip-ui/commit/065ab28c05e67d2df305486309c096d8fd2ae018

The delay's purpose ("allow views that receive compose calls while animating away to find their state") is preserved: entries popped in the meantime are still pruned, only entries that are on the stack right now are kept.

Dominant language
Swift
Stars
334
Forks
95
Avg merge
2d 10h
Merged PRs (30d)
8

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from skiptools/skip-ui

All issues in skiptools/skip-ui

Similar issues

More Swift issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.