Amplify-js: Receiving warning "Setting a timer for a long period of time, i.e. multiple minutes, is a performance and correctness issue on Android"

Created on 12 Mar 2020  路  9Comments  路  Source: aws-amplify/amplify-js

Describe the bug
I'm developing a mobile application using React Native and I'm trying to integrate it with my API using the AWS Amplify SDK for Javascript. It's all working nicely except for a warning I'm getting due an AppSync subscription. Here's the message:
WARN 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)

image

The subscribe function is working despite the warning message, but I think that an improvement should be considered nonetheless if there isn't one already.

Here's the function code that causes the warning:

handleSubscription() {
    let sub = API.graphql({
        query: SubscribeWebSocketResponses,
        variables: { user: this.context.id },
        authMode: GRAPHQL_AUTH_MODE.AWS_IAM
    });
    sub.subscribe({
        next: ({ value: { data: { onWebSocketResponse } } }) => {
            console.log(onWebSocketResponse);
            if (
                // Checking data received
            ) {
                return;
            }
            try {
                // Handling data
            } catch (err) {
                console.log(err);
            }
        },
        error: (error) => {
            console.log(error);
        }
    });
}

These are the dependencies' versions:

  • "amazon-cognito-identity-js": "^3.2.5",
  • "aws-amplify": "^2.2.6",
  • "aws-amplify-react-native": "^3.2.2",

Thanks a lot in advance!

API React Native

Most helpful comment

To fix this issue...

  1. Navigate to your node_modules/react-native/Libraries/Core/Timers/JSTimers.js file.

  2. Look for the variable MAX_TIMER_DURATION_MS

  3. Change its value to 10000 * 1000

Save the changes (with auto format turned off) and re-build your app.

It worked for me

All 9 comments

I'm seeing this problem with subscriptions as well. It's very disruptive to the development process as the warning pops up again and again.

This should be addressable in amplify by breaking the timer into smaller pieces. Setting long timers like this is a big performance no no on android. Not sure why a timer needs to be set for 300000ms (5 minutes). Thats a really long time in a mobile app.

I'm experiencing this also. In lieu of a proper fix, I'm disabling the warning with

import { YellowBox } from 'react-native';
YellowBox.ignoreWarnings(['Setting a timer']);

It's not clear to me whether the underlying issue is something that RN needs to fix, Amplify needs to fix, or is something that I need to patch. Can anyone shed any light?

Thank you for your reply @humatic

Even if your solution works, I don't think it's a good idea to just hide a warning, especially when it can cause performance issues. There are already too many apps that slow down our smartphones due to heavy background processing or stuff like that :thinking: .

This problem happens to me only when I use API subscription, so I think it is something related to keep alive or heartbeats sent to the AWS infrastructure in order to know when the client goes offline.
If I'm right, I think that this is not something we can patch without modifying the AWS amplify library.

Anyway, my temporary work around consists of doing what has been described here, and I think it's working because I'm not seeing the warning anymore.

Even if your solution works, I don't think it's a good idea to just hide a warning, especially when it can cause performance issues.

I agree wholeheartedly. I will also try the temporary work around you linked. Thanks.

@MattiaCostamagna Looks like this issue can be closed since this can be corrected without amplify patching anything. Let me know if thats not the case and I can reopen the issue.

@MattiaCostamagna Looks like this issue can be closed since this can be corrected without amplify patching anything. Let me know if thats not the case and I can reopen the issue.

How can this be corrected? Is it safe to hide the warning as above?

To fix this issue...

  1. Navigate to your node_modules/react-native/Libraries/Core/Timers/JSTimers.js file.

  2. Look for the variable MAX_TIMER_DURATION_MS

  3. Change its value to 10000 * 1000

Save the changes (with auto format turned off) and re-build your app.

It worked for me

I'm still getting this warning
"aws-amplify": "^3.3.14",
"aws-amplify-react-native": "^4.2.7"

Was this page helpful?
0 / 5 - 0 ratings