javascript - firebase 与本地 react ,太慢

标签 javascript firebase react-native

我使用firebase 3.4.1和react-native 0.34.1。

令我惊讶的是,从 Firebase 接收检索到的数据非常慢。

这是我的 react native 测试代码。

class test1 extends Component {
    constructor(){
        super();

        let config = {
            apiKey: "xxxxxxxxxxxxxx",
            authDomain: "yyyyyyyy.firebaseapp.com",
            databaseURL: "https://zzzzzz.firebaseio.com",
        };

        Firebase.initializeApp(config);

        this.rootRef = Firebase.database().ref();
        this.postRef = this.rootRef.child('posts');

        this.state = {
           like : 'like : ',
           comment : 'comment : ',
        };

    componentWillMount(){
        console.debug('componentWillMount');
        let num = 0;
        let time = new Date().getTime();
        this.postRef.orderByKey().limitToLast(10).once('value')
        .then((postsnap) => {
            console.debug(new Date().getTime() - time);
            postsnap.forEach((postItem) => {
               console.debug(num++);
               console.debug(new Date().getTime() - time);
               this.setState({
                  like : this.state.like + postItem.child('like').numChildren() + ', ',
                  comment : this.state.comment + postItem.child('comment').numChildren() + ', ',
               });
            });
         });
     }

     render() {
         return (
            <View style={styles.container}>
                <Text>
                {this.state.like}
                </Text>
                <Text>
                {this.state.comment}
                </Text>
            </View>
         );
     }
}

我的firebase数据库的结构非常简单, “rootref”只有 6 个“帖子”。

componentWillMount
8760
0
8761
1
8766
2
8767
3
8768
4
8769
5
8772

你们有什么想法可以更快地从 Firebase 检索数据吗?

最佳答案

使用单例模式来获取Reference要好得多。您不必每次都调用 Firebase.initializeApp ,而是通过将其放入单独的模块中来使其全局化。然后,将实例化引用导出到 Firebase.database().ref()。并在应用程序的整个生命周期中使用它。

无论如何,我发现,即使我使用这种方法,1 个节点查询 (node/key) 的最小加载时间也约为 200 毫秒,这非常慢。

关于javascript - firebase 与本地 react ,太慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39795909/

相关文章:

javascript - 将 d3 条形图重置为原始数据源时重复轴节点

javascript - Blob URL 上传到 firebase 存储

react-native - 未定义不是对象(评估 'RCTAsyncStorage.multiMerge' (React-native-asyncstorage)

react-native - React Native 无法构建 APK 文件

react-native - 如何在 React Native 中解决 "Error from the server while geocoding...."?

javascript - 如何创建 for 循环以从 JSON 数组中获取数据

javascript - 每个复选框都会触发事件

javascript - 如何使用 JQuery、CSS 减少 div 的宽度,以便减少左侧部分而不是右侧部分?

node.js - 无法使用 Node.js 管理 SDK 将 Firebase 存储中的内容处置覆盖为 'inline'

Firebase CLI 更改托管目标