1

這對我來說是一個奇怪的問題。Firebase:用戶已通過身份驗證,但未啓動數據庫查詢

基本上,我正在構建一個應用程序,該應用程序在登錄並驗證用戶身份後,將啓動一個頁面以從Firebase數據庫加載數據並保存到狀態。

所有工作正常,直到我試圖驗證用戶沒有登錄(要檢查他們是否已經登錄,以便自動調出主頁視圖控制器)。

這是我的授權碼

import Foundation 
import FirebaseAuth 

class AuthUser { 

    func userCheck(completion:(message:String)->()){ 

    var message = "" 

    let user = FIRAuth.auth()?.currentUser 

    if (user != nil) { 

     print("User is already logged in") 

     message = "LoggedIn" 

    } else { 

     print("User is not logged in") 
     message = "notLoggedIn" 

    } 

    completion(message: message) 

    } 
} 

,這是我的導入數據的代碼

import Foundation 
import FirebaseDatabase 
import FirebaseAuth 
import RealmSwift 

class MenuDataRealm { 

    func importData(completion: (message:String)->()){ 

    print("Initiating Menu Data Import...") 

    // Realm 
    let realm = try! Realm() 

    print("Importing...") 

    let ref = FIRDatabase.database().reference() 

    ref.child("Category").observeSingleEventOfType(.Value, withBlock: { (snapshot) in 

     print("Start Menu Data Retrieval") 
.... 

的問題是,在日誌,該日誌停止打印後的「導入...」,並停止那裏。如果我登錄正常(而不是自動驗證用戶),它會通過完美的全功能運行,並在不停止「導入......」

這是正常的登錄日誌

2016-08-25 01:25:05.481 MannaCatering[33636:480265] Configuring the default app. 
2016-08-25 01:25:05.502 MannaCatering[33636:] <FIRAnalytics/INFO> Firebase Analytics v.3200000 started 
2016-08-25 01:25:05.503 MannaCatering[33636:] <FIRAnalytics/INFO> To enable debug logging set the following application argument: - FIRAnalyticsDebugEnabled 
2016-08-25 01:25:05.507: <FIRInstanceID/WARNING> FIRInstanceID AppDelegate proxy enabled, will swizzle app delegate remote notification handlers. To disable add "FirebaseAppDelegateProxyEnabled" to your Info.plist and set it to NO 
2016-08-25 01:25:05.515 MannaCatering[33636:] <FIRAnalytics/INFO> Successfully created Firebase Analytics App Delegate Proxy automatically. To disable the proxy, set the flag FirebaseAppDelegateProxyEnabled to NO in the Info.plist 
2016-08-25 01:25:05.569 MannaCatering[33636:480305] Version 1.0.2 of  Realm is now available: https://github.com/realm/realm-cocoa/blob/v1.0.2/CHANGELOG.md 
User is already logged in 
2016-08-25 01:25:05.817 MannaCatering[33636:] <FIRAnalytics/INFO> Firebase Analytics enabled 
Logging In... with Email:[email protected] and Password:******** 
User is now Logged In 
Redirecting to Loading Data... 
Initiating Menu Data Import... 
Importing... 
Start Menu Data Retrieval 
Completed Menu Data Import 
Accessing Firebase 
Retrieving Details 
User Details Retrieved 
Redirecting to Gallery 

這是當我嘗試在加載時自動驗證用戶的日誌

2016-08-25 01:34:53.080 MannaCatering[33972:485449] Configuring the default app. 
2016-08-25 01:34:53.108 MannaCatering[33972:] <FIRAnalytics/INFO> Firebase Analytics v.3200000 started 
2016-08-25 01:34:53.109 MannaCatering[33972:] <FIRAnalytics/INFO> To enable debug logging set the following application argument: -FIRAnalyticsDebugEnabled 
2016-08-25 01:34:53.111: <FIRInstanceID/WARNING> FIRInstanceID AppDelegate proxy enabled, will swizzle app delegate remote notification handlers. To disable add "FirebaseAppDelegateProxyEnabled" to your Info.plist and set it to NO 
2016-08-25 01:34:53.159 MannaCatering[33972:] <FIRAnalytics/INFO> Successfully created Firebase Analytics App Delegate Proxy automatically. To disable the proxy, set the flag FirebaseAppDelegateProxyEnabled to NO in the Info.plist 
2016-08-25 01:34:53.166 MannaCatering[33972:485629] Version 1.0.2 of Realm is now available: https://github.com/realm/realm-cocoa/blob/v1.0.2/CHANGELOG.md 
User is already logged in 
Redirecting to Loading Data... 
Initiating Menu Data Import... 
Importing... 
2016-08-25 01:34:53.613 MannaCatering[33972:] <FIRAnalytics/INFO> Firebase Analytics enabled 

還有值得一提的另一件事是,它有時工作並通過工作全功能運行,因爲當我驗證的目的AUT omatically。

然而,當我刪除AuthUser.userCheck功能,重新啓動應用程序,通過手動輸入方法登錄,並添加功能回來了,這總是隻發生正確的。

這似乎是在auth函數儘管顯示用戶已登錄,但並非總是如此,因此數據庫查詢不起作用。

但是,我通過將數據庫讀寫規則更改爲true來測試,允許在沒有身份驗證的情況下訪問,但它仍然停留在「正在導入...」中,儘管如此。

我希望這不是太混亂,請告訴我是否需要任何其他信息來了解我的問題。

+0

給你的JSON樹..嘗試使用突破點來找到它在哪個行打破 – Dravidian

回答

0

嘗試導入Firebase,因爲它位於頂部。我相信你不再需要導入FireAuth等等。 Firebase應該保持所有這一切。

import Firebase 

^這就是我需要做的所有事情,以便進行身份驗證,與數據庫交談等。

我不完全理解你的問題,但是如果你在初始化你的用戶和觀察他的信息時遇到了困難。 這是我爲了提取信息和更新當前用戶的文本標籤:

var user: FIRUser! 
func authenticateUser() { 
    self.user = FIRAuth.auth()?.currentUser 
    FIRDatabase.database().reference().users.child(user!.uid).child("userInfo").observeEventType(.Value) { (snapshot: FIRDataSnapshot) in 

     self.fullNameValue = snapshot.value!["fullName"] as! String 

     } 
} 

我希望我可以幫忙。

+0

嗨。它不適合我。這真是一個奇怪的問題,我甚至不能正確解釋它。基本上,在用戶通過身份驗證之後(打印:用戶已經登錄),有時它的行爲就好像用戶沒有登錄一樣,所以它在獲取用戶詳細信息之前就停在那裏。但是,在其他時候,它完美地工作,大多數情況下在我執行手動登錄之後(刪除自動登錄並將其重新添加)。我設法通過路由它登錄認證後它工作得很好。謝謝你的回答。欣賞它。 –

相關問題