Offline Persistence

Offline Support and Query Persistence

With onlineManager wired to NetInfo 2,178 (TanStack Query Fundamentals), TanStack Query 79,069 knows when the phone is offline. By default (networkMode: 'online') a query that would need the network doesn't fail; it pauses and resumes when the connection returns. With the emulator's Wi-Fi and mobile data switched off (adb shell svc wifi disable, svc data disable), opening book 4, which was not cached, and then switching them back on logged:

Output of 165
'online:', false
'book 4: pending, fetch paused,', 'updated 00:00:00'
'online:', true
'book 4: pending, fetch fetching,', 'updated 00:00:00'
'online:', true
'book 4: data, fetch idle,', 'updated 16:13:48'

The screen showed its spinner, not an error, while paused. Mutations pause too. To survive a restart, the cache itself must be saved. The root layout replaces QueryClientProvider with the persisting version:

src/app/_layout.tsx: restoring the query cache at startup (excerpt)TSX
    <PersistQueryClientProvider client={queryClient} persistOptions={{ persister }}
      onSuccess={() => console.log('query cache restored')}>

The persister (@tanstack/react-query-persist-client and @tanstack/query-async-storage-persister, both 5.103.2) saves the cache to AsyncStorage 5,070 and restores it before queries run; data older than maxAge (24 hours by default) is dropped, hence the day-long gcTime. Persisting Zustand State's restart showed 60 books before any request. With the server stopped and the data stale, pull-to-refresh kept the books on screen and reported the failure after the retries:

Output of 166
'catalog: 60 books, status success, fetch fetching', ''
'catalog: 60 books, status error, fetch idle', '(refetch failed: fetch failed: java.net.Connect
Exception: Failed to connect to /10.0.2.2:8150)'

status became error but data stayed, so the catalog replaces the list only on isLoadingError (no data at all). A buster string, such as the app version, discards caches written by older builds.