我使用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/