2016-09-30 136 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毫秒,這非常慢。