设置一个长时间的计时器,即多分钟 [英] Setting a timer for a long period of time, i.e. multiple minutes

查看:1508
本文介绍了设置一个长时间的计时器,即多分钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用firebase身份验证与反应原生登录注册但我得到一个黄色错误:

长时间设置一个定时器,例如多分钟,是Android上的一个性能和正确性问题,因为它使定时器模块保持唤醒,只有当应用程序在前台时才能调用定时器。请参阅( https://github.com/facebook/react-native/issues/12981)了解更多信息。 (看setTimeout持续时间111862ms)

我该如何解决这个问题?

我不想忽视,我想了解这个错误,并用最好的和标准的方式来解决。

这是我的代码:

  export(default)class Login {Component元素
构造函数{
super(道具)
this.state = {
email:'',
密码:'',
响应:''
}
this.signUp = this.signUp.bind(this)
this.login = this.login.bind(this)

async signUp(){
try {
await firebase.auth()。createUserWithEmailAndPassword(this.state.email,this.state.password)
this .setState({
response:'Account Created!'
})
setTimeout((=)=> {
this.props.navigator.push({
id:'App'
})
},1500)
} catch(error){
this .setState(
response:error.toString()
))
}
}
async login(){
try {
等待firebase.auth()。createUserWithEmailAndPassword(this.state.email,this.state.password)
this.setState({
response:'user login in'
})
setTimeout(()=> {
this.props.navigator.push({
id:'App'
})
})

} catch(error){
this.setState({
response:error.toString()
))
}

}
render(){$ b $ (
< View style = {styles.container}>
< View style = {styles.containerInputes}>
< TextInput
placeholderTextColor =grey
placeholder =Email
style = {styles.inputText}
// onChangeText = {(email)=> this.setState({email})}
onChangeText = {(email)=> {console.log(email);}}
/>
< TextInput
placeholderTextColor =gray
placeholder =Password
style = {styles.inputText}
password = {true}
onChangeText = {(password)=> this.setState({password})}
/>
< / View>
< TouchableHighlight
onPress = {this.login}
style = {[styles.loginButton,styles.button]}
>
< Text
style = {styles.textButton}
>登录< / Text>
< / TouchableHighlight>
< TouchableHighlight
onPress = {this.signUp}
style = {[styles.loginButton,styles.button]}
>
< Text
style = {styles.textButton}
>注册< / Text>
< / TouchableHighlight>
< / View>


$ b code
$ b我报告给 Google Firebase小组:( https://github.com/firebase/ firebase-js-sdk / issues / 97

解决方案

rigobcastro 解决了这个问题。他说要在根组件中添加这行代码,如果你问哪里可以添加它,你可以在构造函数方法中添加代码,如下所示:

 构造函数(){
super();

console.ignoredYellowBox = [
'设定计时器'
];

$ / code>


 从'react-native-background-timer'导入BackgroundTimer; 

setTimeout = BackgroundTimer.setTimeout.bind(BackgroundTimer)
setInterval = BackgroundTimer.setInterval.bind(BackgroundTimer)
clearTimeout = BackgroundTimer.clearTimeout.bind(BackgroundTimer)
clearInterval = BackgroundTimer.clearInterval.bind(BackgroundTimer)


I want to use firebase auth with react native for Login and Signup but I got a yellow error:

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 111862ms)

How Can I Fix That?
I don't want to ignore that, I want to understand this error and solve that with the best and Standard way.
And This is my Code:

  export default class Login extends Component {
        constructor(props) {
            super(props)
            this.state = {
                email: '',
                password: '',
                response: ''
            }
            this.signUp = this.signUp.bind(this)
            this.login = this.login.bind(this)
        }
        async signUp() {
            try {
                await firebase.auth().createUserWithEmailAndPassword(this.state.email, this.state.password)
                this.setState({
                    response: 'Account Created!'
                })
                setTimeout(() => {
                    this.props.navigator.push({
                        id: 'App'
                    })
                }, 1500)
            } catch (error) {
                this.setState({
                    response: error.toString()
                })
            }
        }
        async login() {
            try {
                await firebase.auth().createUserWithEmailAndPassword(this.state.email, this.state.password)
                this.setState({
                    response: 'user login in'
                })
                setTimeout(() => {
                    this.props.navigator.push({
                        id: 'App'
                    })
                })

            } catch (error) {
                this.setState({
                    response: error.toString()
                })
            }

        }
        render() {
            return (
                <View style={styles.container}>
                    <View style={styles.containerInputes}>
                        <TextInput
                            placeholderTextColor="gray"
                            placeholder="Email"
                            style={styles.inputText}
                          //  onChangeText={(email) => this.setState({ email })}
                            onChangeText={(email) => {console.log(email);}}
                        />
                        <TextInput
                            placeholderTextColor="gray"
                            placeholder="Password"
                            style={styles.inputText}
                            password={true}
                            onChangeText={(password) => this.setState({ password })}
                        />
                    </View>
                    <TouchableHighlight
                        onPress={this.login}
                        style={[styles.loginButton, styles.button]}
                    >
                        <Text
                            style={styles.textButton}
                        >Login</Text>
                    </TouchableHighlight>
                    <TouchableHighlight
                        onPress={this.signUp}
                        style={[styles.loginButton, styles.button]}
                    >
                        <Text
                            style={styles.textButton}
                        >Signup</Text>
                    </TouchableHighlight>
                </View>
            )
        }
    }

I Reported to Google Firebase Team: (https://github.com/firebase/firebase-js-sdk/issues/97)

解决方案

rigobcastro solved this. he said to add this line of code in the root component and if you ask where you can add it, you can add the code in the constructor method like so:

constructor() {
     super();

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

Or

import BackgroundTimer from 'react-native-background-timer';

setTimeout = BackgroundTimer.setTimeout.bind(BackgroundTimer)
setInterval = BackgroundTimer.setInterval.bind(BackgroundTimer)
clearTimeout = BackgroundTimer.clearTimeout.bind(BackgroundTimer)
clearInterval = BackgroundTimer.clearInterval.bind(BackgroundTimer)

这篇关于设置一个长时间的计时器,即多分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆