title: Firebase RDB presence not working as expected
body: <p>I'm trying to detect when a player is online/offline, but it seems that while swapping characters all characters stay online until the app is closed, not minimized, or idle but actually closed.</p>
  <p>In <code>useEffect</code> cleanup <code>return ()</code> I can add <code>userStatusDatabaseRef.set(isOfflineForDatabase)</code> and it will correctly show the character going offline. However, when I close the app and <code>onDisconnect</code> is called all characters offline state will get updated again.</p>
  <p>So how would I correctly close the connection for a character when it's no longer active?</p>
  <pre><code>useEffect(() =&gt; {
      if (!selectedCharacter) {
          return
      }
  
      console.log('selected ', selectedCharacter)
      const dbRef = database().ref('.info/connected')
      const userStatusDatabaseRef = database().ref('/status/' + selectedCharacter)
      const userStatusFirestoreRef = firestore().doc(`characters/${selectedCharacter}`)
  
      const onOnlineStateChange = snapshot =&gt; {
          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, {merge:true})
              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, {merge:true})
          })
      }
  
      dbRef.on('value', onOnlineStateChange)
      return () =&gt;{
          console.log('calling return &amp; close for', selectedCharacter)
          dbRef.off('value', onOnlineStateChange)
      }
  
  }, [selectedCharacter])
  
  
  let isOfflineForFirestore = {
      state: 'offline',
      last_seen: firestore.FieldValue.serverTimestamp()
  }
  
  let isOnlineForFirestore = {
      state: 'online',
      last_seen: firestore.FieldValue.serverTimestamp()
  }
  
  var isOfflineForDatabase = {
      state: 'offline',
      last_seen: database.ServerValue.TIMESTAMP
  }
  
  var isOnlineForDatabase = {
      state: 'online',
      last_seen: database.ServerValue.TIMESTAMP
  }
  </code></pre>