2017-04-27 76 views
0

如何在設備斷開連接前向Firebase寫入信息,如果用戶斷開與互聯網的連接,我試圖更新Firebase上的值。林不知道是否有方法可以做到這一點。我搜索並找到了onDisconnect,但只有當用戶註銷時纔有所幫助。如果有任何建議,將不勝感激。如果用戶與互聯網斷開連接,Firebase會更新Firebase上的值

func isUserLoggedInSaved(){ 
     if let user = FIRAuth.auth()?.currentUser { 
      if !user.isAnonymous { 

       let path = "rquest/frontEnd/users/\(self.currentUserId()!)" 
       let connectedRef = FIRDatabase.database().reference(withPath: ".info/connected") 

       let presenceRef = FIRDatabase.database().reference(withPath: path).child("isUserLogon") 
       presenceRef.onDisconnectSetValue(false, withCompletionBlock: { (err, fir) in 
        print("Disconnected") 
       }) 
       connectedRef.observe(.value, with: { snapshot in 
        if let connected = snapshot.value as? Bool, connected { 
         self.childRef(path).updateChildValues(["isUserLogon":true]) 
         print("connectedssss") 
         return 
        }else{ 
         print("not connected anymore ") 
         if #available(iOS 10.0, *) { 
          Timer.scheduledTimer(withTimeInterval: 3, repeats: false, block: { (timer) in 
           self.navAlertCustom(viewType: .StatusLine, theme: Theme.warning, duration: 10, location: .top, message: "No Internet Connection", dimBg: false) 
               }) 

          }) 
         } else { 
          // Fallback on earlier versions 
         } 
        } 
       }) 
      } 
     } 
    } 

回答

0

見此文檔

FIRDatabase.database().reference().onDisconnectSetValue(value: Any) 

CMD +點擊onDisconnectSetValue,你會被導航到的文檔: -

確保這個數據當客戶端斷開連接時,位置設置爲指定的值d(由於關閉瀏覽器,導航到新頁面或網絡問題)...

相關問題