简体   繁体   English

React Native-运行并应用“ setInterval”后,应用程序崩溃

[英]React Native - Application crashes after running and applying “setInterval”

I'm here to ask a problem that I couldn't resolve for a day. 我在这里问一个我一天无法解决的问题。

After applying setInterval , my application crashes on physical android phone and in my emulator it shows an error that I'm not familiar. 应用setInterval之后 ,我的应用程序在物理Android手机上崩溃,并且在我的模拟器中显示了我不熟悉的错误。

In physical android phone and emulator, after I log in in application and STAY for a minute in a particular page/screen it will crash or show an error. 在物理Android手机和模拟器后,我登录应用程序并停留在特定页面/屏幕就会死机或显示错误一分钟。

Here's my code 这是我的代码

export default class tables extends Component {
    constructor(props){
        super(props)
        this.state = {
            data: [],
            ....
        }
    }
    fetchData = async () => {
        const response = await fetch('http://192.168.254.***:****/table');
        const json = await response.json();
        this.setState({ data: json });
    }

    componentDidMount = () => {
        this.Set_int = setInterval(() => {
            this.fetchData().then(() => {
                this.setState({ isLoading: false })
            });
        }, 5000);
    }

    componentWillUnmount = () => {
        clearInterval(this.Set_int);
    }

    render() {
        return (
            <View>
                 ....
                 .......
            </View>
        )
    }
}

Here's the error: 这是错误:

在此处输入图片说明

My console.log: 我的console.log:

在此处输入图片说明

2693896 in server log likely refers to response length, and 2.6 Mb JSON response that is parsed to an object can occupy a plenty of RAM. 服务器日志中的2693896可能是指响应长度,并且解析到一个对象的2.6 Mb JSON响应会占用大量RAM。 The data is requested each 5 seconds, regardless of whether the previous request was completed. 不管先前的请求是否完成,每5秒都会请求一次数据。 If a client or a server can't process data at that speed, requests and state updates will accumulate and occupy all available RAM. 如果客户端或服务器无法以这种速度处理数据,则请求和状态更新将累积并占用所有可用RAM。

setInterval shouldn't be generally used with promises because it ignores promise chain. setInterval通常不应与promises一起使用,因为它会忽略promise链。

In order to improve this situation, a new interval should be either scheduled only when state update is completed, or in case 5s intervals should be preserved, the requests could be cancelled with abortable fetch . 为了改善这种情况,应该仅在状态更新完成时安排新的间隔,或者在应保留5s间隔的情况下,可以使用可中止的访存来取消请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM