【问题标题】:where to use firebase onDisconnect in react native在本机反应中在哪里使用firebase onDisconnect
【发布时间】:2020-02-09 22:02:56
【问题描述】:

我正在使用 react native 和 firebase 开发一个测试应用程序,我想通过在应用程序关闭时更新用户文档中的字段来实现存在状态。但是,我不知道在哪里应用断开连接,但我认为它会在 componentWillUnmount 的 App.js 中。我对本机和firebase都有反应,如果我错了,请指导我。下面是我在 App.js 中实现它的方式,但它不起作用。另外,我已经展示了只与这个问题相关的代码。

App.js

      import { Firebase } from './Fire';


      export default class App extends React.Component {
        constructor(props) {
          super(props);
          this.state = {
            offline: 'offline',
          };
        }

        componentWillMount() {
          Firebase.init();
        }

        async componentWillUnmount() {
          const userUid = await Firebase.auth.currentUser.uid;

          const docRef = Firebase.firestore.collection('users').doc(userUid);
          const disconnectRef = docRef.onDisconnect().update({
            presenceStatus: this.state.offline
          });
        }

【问题讨论】:

    标签: reactjs firebase react-native google-cloud-firestore


    【解决方案1】:

    您似乎在这里混淆了两个 Firebase 数据库:

    • 实时数据库是原始的 Firebase 数据库。它支持检测客户端何时在服务器上断开连接,并通过其onDisconnect 处理程序执行操作。

    • Cloud Firestore 是 Firebase 中较新的数据库。它具有完全不同的可扩展性模型,并使用无连接协议在客户端和服务器之间进行通信。因此,它支持onDisconnect 处理程序。

    因此,如果您想使用 onDisconnect,则需要使用 Firebase 实时数据库。除了 Cloud Firestore 之外,您还可以使用它,或者作为它的替代品。

    【讨论】:

    • 非常感谢,找到了在 firebase 上实现在线状态系统的文档
    • 请快速提问。我正在开发一个使用实时跟踪的按需应用程序,该应用程序需要不断更新人们的纬度和经度,我计划为您提供 Firebase Firestore。阅读了一些关于成本如何在一夜之间飙升的可怕文章(在成本计算方面很可怕)。我想知道为此使用 firestore 是否可取,因为我认为它提供的整个 BaaS 涵盖了我需要的一切。如果您能推荐最好的方法,谢谢。
    • 频繁的小更新在实时数据库上可能更具成本效益。但是Firebase pricing page 上的计算器可能是您找到答案的最佳选择。
    【解决方案2】:

    您可以使用AppState 来检查应用程序的状态,它可以是这三个中的任何一个:

    active - 应用正在前台运行。

    background - 应用程序在后台运行。用户是:在主屏幕上的另一个应用程序中 [Android] 在另一个 Activity 上(即使它是由您的应用程序启动的)。

    [iOS] 不活动 - 这是在前台和后台之间转换以及在不活动期间(例如进入多任务视图或来电时)发生的状态。

    这是一个例子:

    import { AppState } from 'react-native';
    
    export default class App extends React.Component {
      state = {
        appState: AppState.currentState,
        offline: 'offline'
      }
    
    
      componentDidMount() {
        AppState.addEventListener('change', this._handleAppStateChange);
      }
    
      componentWillUnmount() {
        AppState.removeEventListener('change', this._handleAppStateChange);
      }
    
      _handleAppStateChange = async (nextAppState) => {
        if (nextAppState.match(/inactive|background/) && this.state.appState === 'active') {
          console.log('App has come to the foreground!');
    
          const userUid = await Firebase.auth.currentUser.uid;
    
          const docRef = Firebase.firestore.collection('users').doc(userUid);
    
          const disconnectRef = docRef.onDisconnect().update({
            presenceStatus: this.state.offline
          });
        }
    
        this.setState({appState: nextAppState});
      }
    }
    

    有关AppState的更多详细信息,请转到here

    【讨论】:

    • 谢谢,但这不会检测应用程序何时关闭,它只会检测它何时处于后台。我还问如何使用firebase firestore onDisconnect来实现它。谢谢
    猜你喜欢
    • 2018-09-26
    • 1970-01-01
    • 2019-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-20
    • 2019-06-01
    相关资源
    最近更新 更多