Optimistic updates do not seem to work as fast expected in Firestore. I am seeing update times at a minimum of 200 ms, in between the time I write to Firestore and the time my snapshot listeners get triggered. I am using the react native firebase library, but I think it unlikely the issue originate there. The library is simply a thin wrapping layer to the native SDKs. I have offline persistence enabled for what that's worth, but the behavior is the same with or without internet connectivity.
The following code snippet illustrates the issue:
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>
}
}
Is there anything I am doing wrong or could do differently? I love the pattern of subscribing to snapshots from the data store and rendering the UI directly from it, then writing directly to the datastore and having my UI instantly update via the optimistic propagation.