繁体   English   中英

具有响应本机的侦听器上的Firebase

[英]Firebase on listener with react native

所以我试图附加一个.on监听器,像这样

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

问题是,我收到一条错误消息:

长时间设置计时器(即数分钟)是Android上的性能和正确性问题,因为它会使计时器模块保持唤醒状态,并且只有在应用程序处于前台时才能调用计时器。 有关更多信息,请参见https://github.com/facebook/react-native/issues/12981 (看到setTimeout持续时间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'
        );
    };
}

您可以在下面为您的组件尝试

 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)
            })
}

暂无
暂无

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

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