亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

檢查消防倉庫的實時變化

檢查消防倉庫的實時變化

狐的傳說 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 關注
  • 110 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號