Sco*_*mas 5 firebase react-native google-cloud-firestore react-native-firebase
Firestore 中的乐观更新似乎没有预期的那么快。我看到更新时间至少为 200 毫秒,在我写入 Firestore 的时间和触发快照侦听器的时间之间。我正在使用 React Native Firebase 库,但我认为问题不太可能源于此。该库只是本机 SDK 的一个薄包装层。我启用了离线持久性,但无论是否有互联网连接,行为都是相同的。
下面的代码片段说明了这个问题:
class App extends React.Component{
componentDidMount(){
db.collection("cities").onSnapshot(function(qsnap) {
this.setState({cities: qsnap.docs.map(s => s.data())})
});
}
addCity(){
db.collection("cities").add({
name: Math.random(),
country: "Japan"
})
}
render(){
//After clicking add city, the new render doesn't happen until
//after a minimum time of 200 ms :(
<View>
<TouchableOpacity onPress={this.addCity}>Add city</TouchableOpacity>
{this.state.cities.map(c => <Text>{c.name}</Text>)}
</View>
}
}
Run Code Online (Sandbox Code Playgroud)
我有什么做错的地方或者可以采取不同的做法吗?我喜欢从数据存储订阅快照并直接从中渲染 UI,然后直接写入数据存储并通过乐观传播让我的 UI 立即更新的模式。
| 归档时间: |
|
| 查看次数: |
1094 次 |
| 最近记录: |