【发布时间】: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