我正在地图项目中实时使用 firestore,它需要以 x 距离间隔更新用户的当前位置。但是,实时侦听器不断重新获取我自己的更新,从而增加了我的阅读量。我假设 firestore 在发送到服务器之前会实时更新本地缓存,是否可以忽略该用户所做的更改?class Booking extends Component { constructor(props) { super(props); this.state = { isLoading: false, errorMessage: '', }; this.unsubscribe = null; } componentDidMount() { this.getRealTimeData(); } componentWillUnmount() { this.unsubscribe = null; } getRealTimeData = () => { this.fetchCompletedGigs(); } fetchCompletedGigs = () => { const { userID } = this.props; this.props.bookingData([]); this.setState({ isLoading: true, errorMessage: '' }); this.unsubscribe = Firebase.shared.fetchBooking('Bookings') .where('userID', '==', userID) .orderBy('d.CreatedAt', 'desc') .limit(20) .onSnapshot((querySnapshot) => { if (querySnapshot.empty) { this.setState({ isLoading: false, errorMessage: "You currently don't have anybooking", }); this.props.bookingData([]); } querySnapshot.docChanges().forEach(change => { const doc = change.doc; const item = doc.data(); item.docId = doc.id; const list = [...this.props.bookings, item]; this.setState({ isLoading: false, errorMessage: '' }); if (change.type === 'added') { const filterList = _.uniqBy(list, 'docId'); this.props.bookingData(filterList); } else if (change.type === 'removed') { const newData = list.filter(task => task.docId !== doc.id); const filterList = _.uniqBy(newData, 'docId'); return this.props.bookingData(filterList); } else if (change.type === 'modified') { const newData = list.filter(task => task.docId !== doc.id); const newList = [...newData, item]; const filterList = _.uniqBy(newList, 'docId'); return this.props.bookingData(filterList); } }
1 回答

桃花长相依
TA贡献1860条经验 获得超8个赞
您无法阻止侦听器触发本地事件。但是,您可以在回调中检测到这些本地事件,并在那里忽略它们:onSnapshot
Firebase.shared.fetchBooking('Bookings')
.where('userID', '==', userID)
.orderBy('d.CreatedAt', 'desc')
.limit(20)
.onSnapshot((querySnapshot) => {
...
querySnapshot.docChanges().forEach(change => {
if (change.doc.metadata.hasPendingWrites) {
... handle the local event differently
}
else {
... handle normally
}
});
...
});
另请参阅有关检测本地更改的 Firebase 文档。
添加回答
举报
0/150
提交
取消