T
TanStack3y ago
wise-white

If the mobile app goes to sleep, on resuming the cache is cleared and the app crashes.

Hey there, I'm currently working on a React Native app and I'm trying to solve for the case where an app went to sleep in the background and was resumed. I'm currently using the 5 min default cache for my cache and refetching on screen focus with the React Navigation focus hook. To prevent the crash from happing I was thinking of two solutions. One use a client side state management as well and fetch when stale like normal. Second, I could set the cache to Infinity for the few queries I never want garage collected. Do these solutions sound reasonable for this case? Would love some thoughts on this.
6 Replies
absent-sapphire
absent-sapphire3y ago
Why does the app crash if the cache is empty?
wise-white
wise-whiteOP3y ago
Sorry for the late reply. I get a, for example, userProfile.id is undefined and it errors out Maybe I’m not understanding cache correctly. When the cache expires, does the data that’s in “state” get removed? Or is it still there to be used by the UI
absent-sapphire
absent-sapphire3y ago
Data that is actively used will not be removed. If it is unused it stays in the cache until cacheTime expires (15 min per default)
wise-white
wise-whiteOP3y ago
When you say actively used, do you mean because queries that get refreshed on focus? So that refreshes the cache time? I'm using React Native and there's no automatic focus refresh but I know how to set that up with React Navigation. Otherwise, if there's never a focus event, the query never gets refetched and the cache expires. I guess that's what I'm trying to understand/handle. If a focus event never occurs in a mobile app and the cache expires, the data the UI is using goes with it?
absent-sapphire
absent-sapphire3y ago
actively used mean "rendered on the screen with useQuery"
wise-white
wise-whiteOP3y ago
Ok, that helps me understand it better. Thank you for your help.

Did you find this page helpful?