为了账号安全,请及时绑定邮箱和手机立即绑定

检查消防仓库的实时变化

检查消防仓库的实时变化

狐的传说 2022-09-23 21:30:59
我正在地图项目中实时使用 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 文档。


查看完整回答
反对 回复 2022-09-23
  • 1 回答
  • 0 关注
  • 64 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信