query icon indicating copy to clipboard operation
query copied to clipboard

React Native Setting a timer for a long period of time warning

Open vikrantnegi opened this issue 6 years ago • 10 comments

Describe the bug I'm using react query in react native to fetch the geolocation coordinates. It iseems to be working fine but giving a warning in the console Setting a timer for a long period of time.

To Reproduce Steps to reproduce the behavior:

  1. Create React Native CLI Project
  2. Install react-native-geolocation-service
  3. create a react query for fetching location

Expected behavior When I try to mimic the react query using custom hooks it doesn't give the timer warning.

Smartphone (please complete the following information):

  • Device: Android emulator, Pixel_2_API_29
  • OS: macOS Catalina
  • "react": "16.13.1",
  • "react-native": "0.63.3",
  • "react-query": "^2.26.1"

Code:

async function getPosition() {
  const hasLocationPermission = await checkLocationPermission();

  if (!hasLocationPermission) {
    return;
  }

  return new Promise((res, rej) => {
    Geolocation.getCurrentPosition(res, rej);
  });
}

const App = () => {
  const info = useQuery('pos', getPosition);
  console.log({info});
}

Warning:

Setting a timer for a long period of time, i.e. multiple minutes, is a performance and correctness issue on Android as it keeps the timer module awake, and timers can only be called when the app is in the foreground. See https://github.com/facebook/react-native/issues/12981 for more info.
(Saw setTimeout with duration 300000ms) 

vikrantnegi avatar Nov 05 '20 05:11 vikrantnegi

@vikrantnegi Yeah, this has been a longstanding issue with React-Native for a while now. you can check out their recommendations at: https://github.com/facebook/react-native/issues/12981#issuecomment-652745831

In the case of react-query, I believe this is related to the default cacheTime of 5 minutes. I don't think it should affect your app all that much, given that the garbage collection occurs if you have the app in the foreground anyway.

dominictwlee avatar Nov 05 '20 20:11 dominictwlee

@dominictwlee I see. As long as it doesn't affect the app performance I'm fine with it. Thanks for the info.

vikrantnegi avatar Nov 06 '20 04:11 vikrantnegi

I get that this is a react-native problem, but can you guys suggest how to disable the warning specifically for react-query? It spams my logs with huge stack traces and the real logs get buried in there.

dan-lee avatar Dec 15 '20 22:12 dan-lee

I get that this is a react-native problem, but can you guys suggest how to disable the warning specifically for react-query? It spams my logs with huge stack traces and the real logs get buried in there.

console.ignoredYellowBox = ['Setting a timer'];

dominictwlee avatar Dec 15 '20 23:12 dominictwlee

@dominictwlee Thanks, but that disables it entirely. I wanted to specifically exclude react-query, because this warning could be helpful in other places.

dan-lee avatar Dec 16 '20 09:12 dan-lee

@dominictwlee Thanks, but that disables it entirely. I wanted to specifically exclude react-query, because this warning could be helpful in other places.

Don't believe that's possible, since it's a react native warning. Nothing you can do on the react-query side.

dominictwlee avatar Dec 16 '20 09:12 dominictwlee

I got this warning when I tried to set cacheTime to 1800000. Will it work irrespective of the warning?

YajanaRao avatar Mar 06 '21 17:03 YajanaRao

I got this warning when I tried to set cacheTime to 1800000. Will it work irrespective of the warning?

Yep you should be fine. For more details read: https://github.com/facebook/react-native/issues/12981#issuecomment-652745831

dominictwlee avatar Mar 06 '21 18:03 dominictwlee

From RN v0.63, Use LogBox instead of console.ignoredYellowBox

LogBox.ignoreLogs(['Setting a timer']);

velopert avatar Mar 15 '21 18:03 velopert

I believe this warning will no longer be shown in upcoming RN 0.65, see: https://github.com/facebook/react-native/commit/480dabd66547a60522249eda203a3eb1934b02e5 & https://github.com/react-native-community/releases/blob/e0636e5aa858320e9695a9c252849d5c97783507/CHANGELOG.md#removed

badsyntax avatar Aug 10 '21 11:08 badsyntax