Javascript Firebase基于具有react-native的侦听器

Javascript Firebase基于具有react-native的侦听器,javascript,android,firebase,react-native,firebase-realtime-database,Javascript,Android,Firebase,React Native,Firebase Realtime Database,所以我尝试在侦听器上附加一个.on,如下所示 firebase.database().ref('Users').child('AhvRcIT2anTaucSDoOgt2MLNxgZ2').on('value', snap => { const user = snap.val(); alert(true); }).catch(e => alert(e)) 问题是,我在说 在Android上,设置一个长时间的计时器(即多分钟)是一个性能和正确性问题,因为它会使计时器模块

所以我尝试在侦听器上附加一个.on,如下所示

firebase.database().ref('Users').child('AhvRcIT2anTaucSDoOgt2MLNxgZ2').on('value', snap => {
    const user = snap.val();
    alert(true);
}).catch(e => alert(e))
问题是,我在说

在Android上,设置一个长时间的计时器(即多分钟)是一个性能和正确性问题,因为它会使计时器模块处于唤醒状态,并且计时器只能在应用程序位于前台时调用。有关更多信息,请参阅。(Saw设置超时,持续时间为398331ms)

我想这是有道理的。我能找到的唯一解决办法就是隐藏警告,这听起来是个坏主意。特别是当我添加这个监听器后,我的应用程序开始冻结

我知道有可用的
react native firebase
,但我已经阅读了它所做的一切,只是隐藏了警告,并没有真正解决问题

但是这个问题怎么解决呢?还是Android上就必须是这样

全家庭式

export default class HomeScreen extends React.Component {
    static navigationOptions = {
        header: null,
    };

    componentWillMount() {
        (async () => {
            await firebase.auth().signInAndRetrieveDataWithEmailAndPassword('loigin', 'pass');
            const val = await firebase.database().ref('Users').child('AhvRcIT2anTaucSDoOgt2MLNxgZ2').once('value').then(r => r.val()).catch(e => alert(e));
            alert(val);
        })();
    }


    render() {
        // firebase.database().ref('Users').child('AhvRcIT2anTaucSDoOgt2MLNxgZ2').on('value', snap => {
        //     const user = snap.val();
        //     alert(true);
        // }).catch(e => alert(e))
        alert(false)
        return (
            <View style={styles.container}>
                <ScrollView style={styles.container} contentContainerStyle={styles.contentContainer}>
                    <View style={styles.welcomeContainer}>
                        <Image
                            source={
                                __DEV__
                                    ? require('../assets/images/robot-dev.png')
                                    : require('../assets/images/robot-prod.png')
                            }
                            style={styles.welcomeImage}
                        />
                    </View>

                    <View style={styles.getStartedContainer}>
                        {this._maybeRenderDevelopmentModeWarning()}

                        <Text style={styles.getStartedText}>Get started by opening</Text>

                        <View style={[styles.codeHighlightContainer, styles.homeScreenFilename]}>
                            <MonoText style={styles.codeHighlightText}>screens/HomeScreen.js</MonoText>
                        </View>

                        <Text style={styles.getStartedText}>
                            Change this text and your app will automatically reload.
                        </Text>
                    </View>

                    <View style={styles.helpContainer}>
                        <TouchableOpacity onPress={this._handleHelpPress} style={styles.helpLink}>
                            <Text style={styles.helpLinkText}>Help, it didn’t automatically reload!</Text>
                        </TouchableOpacity>
                    </View>
                </ScrollView>

                <View style={styles.tabBarInfoContainer}>
                    <Text style={styles.tabBarInfoText}>This is a tab bar. You can edit it in:</Text>

                    <View style={[styles.codeHighlightContainer, styles.navigationFilename]}>
                        <MonoText style={styles.codeHighlightText}>navigation/MainTabNavigator.js</MonoText>
                    </View>
                </View>
            </View>
        );
    }

    _maybeRenderDevelopmentModeWarning() {
        if (__DEV__) {
            const learnMoreButton = (
                <Text onPress={this._handleLearnMorePress} style={styles.helpLinkText}>
                    Learn more
                </Text>
            );

            return (
                <Text style={styles.developmentModeText}>
                    Development mode is enabled, your app will be slower but you can use useful development
                    tools. {learnMoreButton}
                </Text>
            );
        } else {
            return (
                <Text style={styles.developmentModeText}>
                    You are not in development mode, your app will run at full speed.
                </Text>
            );
        }
    }

    _handleLearnMorePress = () => {
        WebBrowser.openBrowserAsync('https://docs.expo.io/versions/latest/guides/development-mode');
    };

    _handleHelpPress = () => {
        WebBrowser.openBrowserAsync(
            'https://docs.expo.io/versions/latest/guides/up-and-running.html#can-t-see-your-changes'
        );
    };
}
导出默认类主屏幕扩展React.Component{
静态导航选项={
标题:null,
};
组件willmount(){
(异步()=>{
等待firebase.auth().SignandRetrieveDatawithEmailandPassword('loigin','pass');
const val=wait firebase.database().ref('Users').child('AhvRcIT2anTaucSDoOgt2MLNxgZ2')。一次('value')。然后(r=>r.val()).catch(e=>alert(e));
警报(val);
})();
}
render(){
//firebase.database().ref('Users').child('AhvRcIT2anTaucSDoOgt2MLNxgZ2').on('value',snap=>{
//const user=snap.val();
//警惕(正确);
//}).catch(e=>alert(e))
警报(错误)
返回(
{this.\u可能是UnderDevelopmentModeWarning()}
从打开开始
screens/HomeScreen.js
更改此文本,您的应用程序将自动重新加载。
救命,它没有自动重新加载!
这是一个选项卡栏。您可以在以下位置编辑它:
导航/MainTabNavigator.js
);
}
_可能是开发模式警告(){
如果(开发){
常量learnmrubtton=(
了解更多
);
返回(
开发模式已启用,您的应用程序将变慢,但您可以使用有用的开发模式
工具{learnmrubtton}
);
}否则{
返回(
您未处于开发模式,您的应用程序将全速运行。
);
}
}
_handleLearnMorePress=()=>{
WebBrowser.openBrowserAsync('https://docs.expo.io/versions/latest/guides/development-mode');
};
_handleHelpPress=()=>{
WebBrowser.openBrowserAsync(
'https://docs.expo.io/versions/latest/guides/up-and-running.html#can-t-see-your-changes'
);
};
}

您可以在下面尝试使用组件willmount

 componentWillMount() {
            firebase
            .auth()
            .createUserWithEmailAndPassword('loigin', 'pass')
            .then(() => {

                firebase.database()
                    .ref('Users')
                    .child('AhvRcIT2anTaucSDoOgt2MLNxgZ2').on('value', function (snapshot) {
                        alert(snapshot.val())
                    })
                    .catch(e => alert(e));

            })
            .catch(error => {
                alert(error.message)
            })
}

你能分享更多的代码吗?我添加了整个组件。好的,但我需要应用程序来监听数据库中的更改
Once
只获取数据库节点的当前状态。我更新了答案。你可以用on代替one,这和我举的例子有什么不同?它只是使用then而不是wait。还是我遗漏了什么?+警告依然存在