2016-09-30 141 views
0

我使用firebase 3.4.1和反应原生0.34.1。firebase与反应原生,太慢

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

这是我的反应本机测试代码。

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

我的火力数据库的结构非常简单, 'rootref' 仅拥有6 '帖子'。

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

你们是否有从火力更快的检索数据的任何想法?

+0

数据检索性能通常是您请求的数据量的组合,然后将其除以您可用的带宽。这可能是因为您的React Native设置有问题,但我们无法为此做任何事情。你能设置一个定期的React jsbin/jsfiddle来重现问题吗? –

+0

它似乎是连接问题,..我想.. 连接后没有成本时间。 –

回答

0

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

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