与firestore本地反应得到在线用户数

时间:2018-04-16 15:01:16

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

我使用firebase / firestore构建应用本地应用。 我正在寻找一种方法来检查应用中的在线用户数量,因此我找到了一种方法this tutorial

var userRef = new Firebase('https://<demo>.firebaseio.com/presence/' + userid);
userRef.on('value', function(snapshot) {
  if (snapshot.val() === true) {
    // User is online, update UI.
  } else {
    // User logged off at snapshot.val() - seconds since epoch.
  }
});

我正在寻找与firestore相关的方法并做出反应。有什么实现我可以看到它是怎么做的?

我发现这种方式与firestore

有关
import { Platform } from 'react-native';
import firebase from 'react-native-firebase';


function rtdb_and_local_fs_presence() {
    // [START rtdb_and_local_fs_presence]
    // [START_EXCLUDE]
    var uid = firebase.auth().currentUser.uid;
    console.log('uid',uid)
    var userStatusDatabaseRef = firebase.database().ref('status/' + uid);

    var isOfflineForDatabase = {
        state: 'offline',
        last_changed: firebase.database.ServerValue.TIMESTAMP,
    };

    var isOnlineForDatabase = {
        state: 'online',
        last_changed: firebase.database.ServerValue.TIMESTAMP,
    };

    // [END_EXCLUDE]
    var userStatusFirestoreRef = firebase.firestore().doc('status/' + uid);

    // Firestore uses a different server timestamp value, so we'll 
    // create two more constants for Firestore state.
    var isOfflineForFirestore = {
        state: 'offline',
        last_changed: firebase.firestore.FieldValue.serverTimestamp(),
    };

    var isOnlineForFirestore = {
        state: 'online',
        last_changed: firebase.firestore.FieldValue.serverTimestamp(),
    };

    firebase.database().ref('.info/connected').on('value', function(snapshot) {
        if (snapshot.val() == false) {
            // Instead of simply returning, we'll also set Firestore's state
            // to 'offline'. This ensures that our Firestore cache is aware
            // of the switch to 'offline.'
            userStatusFirestoreRef.set(isOfflineForFirestore);
            return;
        };

        userStatusDatabaseRef.onDisconnect().set(isOfflineForDatabase).then(function() {
            userStatusDatabaseRef.set(isOnlineForDatabase);

            // We'll also add Firestore set here for when we come online.
            userStatusFirestoreRef.set(isOnlineForFirestore);
        });
    });
    // [END rtdb_and_local_fs_presence]
}

function fs_listen() {
    var uid = firebase.auth().currentUser.uid;
    var userStatusFirestoreRef = firebase.firestore().doc('status/' + uid);

    // [START fs_onsnapshot]
    userStatusFirestoreRef.onSnapshot(function(doc) {
        var isOnline = doc.data().state == 'online';
        // ... use isOnline
    });
}


firebase.auth().signInAnonymouslyAndRetrieveData().then((user) => {
    rtdb_and_local_fs_presence();
    fs_listen();
});

当我在线时,它确实使用正确的uid更新状态集合,但是当我从应用程序断开连接时,它不会更新为脱机状态。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

当用户完全关闭应用程序时它将起作用。

Sub Color()
 Dim iSrs As Long, nSrs As Long
 If ActiveChart Is Nothing Then
  MsgBox "No chart selected. Please try again.", vbExclamation, "No Active Chart"
 Else
  With ActiveChart
   nSrs = .SeriesCollection.Count
   For iSrs = 1 To nSrs
    ' only format series whose names are found
    Select Case LCase$(.SeriesCollection(iSrs).Name)

        Case "dormakaba" '******CHANGE HERE******
            .SeriesCollection(iSrs).Format.Fill.ForeColor.RGB = RGB(255, 130, 171)
            .SeriesCollection(iSrs).Format.Line.Visible = True
            .SeriesCollection(iSrs).Format.Line.Visible = False
        Case "georg fischer" '******CHANGE HERE******
            .SeriesCollection(iSrs).Format.Fill.ForeColor.RGB = RGB(155, 48, 255)
            .SeriesCollection(iSrs).Format.Line.Visible = True
            .SeriesCollection(iSrs).Format.Line.Visible = False
        Case "clariant" '******CHANGE HERE******
            .SeriesCollection(iSrs).Format.Fill.ForeColor.RGB = RGB(0, 255, 0)
            .SeriesCollection(iSrs).Format.Line.Visible = True
            .SeriesCollection(iSrs).Format.Line.Visible = False
        Case "givaudan" '******CHANGE HERE******
            .SeriesCollection(iSrs).Format.Fill.ForeColor.RGB = RGB(202, 225, 255)
            .SeriesCollection(iSrs).Format.Line.Visible = True
            .SeriesCollection(iSrs).Format.Line.Visible = False
        Case "galencia" '******CHANGE HERE******
            .SeriesCollection(iSrs).Format.Fill.ForeColor.RGB = RGB(67, 205, 128)
            .SeriesCollection(iSrs).Format.Line.Visible = True
            .SeriesCollection(iSrs).Format.Line.Visible = False
        Case "lonza" '******CHANGE HERE******
            .SeriesCollection(iSrs).Format.Fill.ForeColor.RGB = RGB(238, 230, 133)
            .SeriesCollection(iSrs).Format.Line.Visible = True
            .SeriesCollection(iSrs).Format.Line.Visible = False

     End Select
    Next
   End With
  End If
 End Sub