【问题标题】:firebase with react native, too slow带有本机反应的firebase,太慢了
【发布时间】:2016-09-30 16:19:05
【问题描述】:

我使用 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 检索数据有什么想法吗?

【问题讨论】:

  • 数据检索性能通常是您请求的数据量然后除以您可用的带宽的组合。可能是您的 React Native 设置有问题,但我们对此无能为力。你能设置一个重现问题的常规 React jsbin/jsfiddle 吗?
  • 好像是连接问题,..我想..连接后没有花费时间。

标签: javascript firebase react-native


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2020-09-04
    • 1970-01-01
    • 2019-05-08
    • 1970-01-01
    • 2021-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-16
    相关资源
    最近更新 更多