Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native 用户关闭应用程序时发送Firebase帖子_React Native_Firebase Realtime Database_Expo - Fatal编程技术网

React native 用户关闭应用程序时发送Firebase帖子

React native 用户关闭应用程序时发送Firebase帖子,react-native,firebase-realtime-database,expo,React Native,Firebase Realtime Database,Expo,我目前有发送用户位置的post路由,当应用程序打开时,isOnline:true;当用户关闭应用程序时,我想更新常规数据,并将isOnline:false更新到我的Firebase Realtime DB。我尝试使用控制台日志来确定卸载发生的确切时间,但它的一致性不足以告诉我。也许我对组件如何卸载的看法是错误的 (如果这是一个重复的问题,我很抱歉。在我决定发布这篇文章之前,我找了大约一个小时) 这是我的卸载: componentWillUnmount() { this.userDisc

我目前有发送用户位置的post路由,当应用程序打开时,isOnline:true;当用户关闭应用程序时,我想更新常规数据,并将isOnline:false更新到我的Firebase Realtime DB。我尝试使用控制台日志来确定卸载发生的确切时间,但它的一致性不足以告诉我。也许我对组件如何卸载的看法是错误的

(如果这是一个重复的问题,我很抱歉。在我决定发布这篇文章之前,我找了大约一个小时)

这是我的卸载:

componentWillUnmount() {

    this.userDisconnectHandler();

    navigator.geolocation.clearWatch(this.watchID);

    this.stopTimer();
  }
这是我的断开连接处理程序:

userDisconnectHandler() {
    var postDataRef = firebase.database().ref(`live-data/${userName}`);
    console.log("disconnected")

    navigator.geolocation.getCurrentPosition(
      (position) => {
        postDataRef
          .set({
        coordinate: {
          longitude: position.coords.longitude,
          latitude: position.coords.latitude,
        },
        userName: `${userName}`,
        isOnline: false,
          })
          .then(() => console.log("finalPostComplete"))
          .catch((err) => console.log(err));
      },
      (err) => console.log(err)
    );
  }

您可能想尝试AppState

据博士说

AppState可以告诉你应用程序是在前台还是后台, 并在状态更改时通知您

AppState经常用于确定目的和正确性 处理推送通知时的行为


这应该适合您的情况。

您可能需要尝试应用程序状态
https://reactnative.dev/docs/appstate
@SaachiTech将此作为一个解决方案发布,这样我就可以为您提供解决问题的积分。谢谢