← Back to Pull requests

Start the refresh on open as soon as the network is connected #5583

Open
unknown opened this pull request with 621 lines diff across 7 files
View on GitHub
unknown unknown commented on Nov 05, 2026 14:42 UTC
Author

PCIOS-1070 · ●●● High · logs · 2/3 pass

Part of PCIOS-1070. Goes straight to trunk and doesn't depend on the other PRs.

The refresh on open always started 2 seconds after the app became active, to give the network time to come up. Now it starts as soon as the network is connected, which is right away when it already is. When it isn't, it still waits at most 2 seconds.

Gain: about 2 s off every open that refreshes, for every library size. With the heavy staging account (545 podcasts, 315K episodes), an open with nothing new should go from 6.5 s to about 4.5 s from process start to synced, and an open after 14 days away from 16.9 s to about 14.9 s. This is an estimate. In every open measured on trunk (optimized build, iPhone 17 Pro simulator, medians of 4–5 runs), the refresh started exactly 2.0 s after the app became active, and the refresh and the sync after it all wait on that start. On this branch the refresh started 5–166 ms after the app became active (signed out, see below). I didn't measure the open times again.

Risk: ●●● High — the refresh that runs on every open after 5 minutes away.

  • The refresh now starts while the first screen is still loading, so its network and database work overlaps with the launch.

  • On a resume where the network isn't ready yet but the monitor still has the last connected path, the refresh starts at once and can fail where waiting 2 s might have let it through. A failed refresh doesn't update the last refresh time, so the next open tries again.

  • NetworkUtils gets whenConnected(timeout:perform:), which holds the callback until the network connects or the timeout passes. Only the refresh on open uses it.
  • refreshOnOpenWithoutDelay (on by default) turns this off remotely.

    Root cause and fix

    AppDelegate.handleBecomeActive() started the refresh from a 2-second Timer, so it started 2 s after the app became active even when the network was already up. It now calls NetworkUtils.shared.whenConnected(timeout:) with the same 2 s. The refresh starts as soon as the NWPathMonitor path is satisfied, or after 2 s if it isn't by then, so it never starts later than before. The 5-minute check and the branch that only checks for pending downloads and uploads are unchanged.

    Checking isConnected() alone wasn't enough. NetworkUtils starts its monitor lazily, and the path is unsatisfied until the first update arrives. On a cold launch where nothing else had used NetworkUtils yet, the check in handleBecomeActive() was its first use, so it would have fallen back to the 2 s timer.

    How to verify

    1. Sign in, force quit the app, wait more than 5 minutes and open it. In the debug log or the Xcode console, Refresh - Started) comes right after the app opens (on a cold launch, right after NetworkMonitor: Network is connected), not 2 s later.
    2. Leave the app in the background for more than 5 minutes and come back. The refresh starts right away.
    3. On a device, turn on Airplane Mode, wait 5 minutes and open the app. The refresh starts after 2 s and fails, as on trunk. Turn Airplane Mode off, go to the home screen and come back after 15 seconds. The refresh starts right away and works.
    4. Turn off refreshOnOpenWithoutDelay in Beta Features and repeat step 1. The refresh starts 2 s after the app opens, as on trunk.

    What I ran

  • [pass] Throwaway timing logs (iPhone 17 Pro simulator, iOS 27.2, StagingDebug, signed out). With the flag off, the refresh started 2.000 s after the app became active. With it on: 11 ms on the first launch after installing, where another launch task had already started the monitor; 166 ms on a later cold launch, where the monitor started in handleBecomeActive() with an unsatisfied path while the main queue was still busy with the launch; 5 ms when coming back from the background.

  • [pass] Built Pocket Casts Staging and ran PocketCastsDataModelTests, PocketCastsServerTests and PocketCastsUtilsTests on this branch (iPhone 17 Pro simulator, iOS 27.2): 818 passed, 0 failed, including a new NetworkUtilsTests test that the callback runs once, on the main queue

  • [not run] Open timings with the heavy account
  • Not verified

  • A signed-in account or a large library on this branch. The timings above are from a signed-out simulator, where the refresh has no podcasts.

  • Opening with no network, or with a network that's still coming up. The simulator always has the Mac's network, so the 2 s fallback didn't run in the app.

  • CarPlay, which calls the same handleBecomeActive() when its scene becomes active.
  • Checklist

  • I have considered if this change warrants user-facing release notes and have added them to CHANGELOG.md if necessary.

  • I have considered adding unit tests for my changes.

  • I have updated (or requested that someone edit) the Event Horizon schema to reflect any new or changed analytics.
  • Diff: 621 lines (+465 / -156) across 7 files Reviewer: claude[bot] Review duration: 7m
    All Vouch review confidence checks passed
    Residual risk 0.35 = Change Risk (0.50) × (1 − Review Confidence (0.30)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 621 lines diff across 7 files
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 51% · Reviewer familiarity: 35%
    29%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.385
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #5583"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: medium confidence: 30.0% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 50.3% (MEDIUM) · Higher = larger blast radius
    50.3%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 30.0% · Lower = rushed or shallow review
    30.0%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 35.2% · Moderate risk
    35.2%