【问题标题】:RTDB- How to cleanup .off() of listener .on complex ( listener .on in listener .on('child_add...)RTDB-如何清理 listener .on 复杂的 .off() ( listener .on in listener .on('child_add...)
【发布时间】:2021-11-01 06:07:31
【问题描述】:

使用 Firebase 实时数据库 有什么方法可以清理嵌套在 listener .on() 中的 listener .on() 吗?

useEffect(() => {

    const ChannelRef = database().ref(`/user/${uid}/channel`);
    ChannelRef.keepSynced(true);
    ChannelRef.on('child_added', channel_id => {
          var startTime = ....
          const threadRef = database().ref(`/channel/${channel_id.key}/Thread`).orderByKey().startAt(startTime);
          threadRef.keepSynced(true);
          threadRef.on('value', thread => {.     <--------------------------------   How to cleanup this listenner ?
            dispatch({
               type: 'AddThread',
               payload: thread.val()
            });
    })


    return () => {
          // CleanUp listenner
           ChannelRef.off();
           ChannelRef.keepSynced(false); 
         
          **// How to cleanup database().ref(`/channel/${channel_id.key}/Thread`).orderByKey().startAt(startTime) ?**
   }
  
}, []

【问题讨论】:

    标签: javascript react-native firebase-realtime-database


    【解决方案1】:

    管理此类听众并没有什么特别之处。您需要保留您附加的侦听器列表,然后在效果到期时对它们调用off

    【讨论】:

      猜你喜欢
      • 2014-01-16
      • 2014-11-22
      • 1970-01-01
      • 2023-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多