这对我来说是一个奇怪的问题。
基本上,我正在构建一个应用程序,在登录并验证用户身份后,它将打开一个页面,从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:caieylau@gmail.com 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
还有一件值得一提的事情是,它有时可以正常工作,并且在我自动进行身份验证时运行整个函数。
但是,这只会在我删除AuthUser.userCheck函数,重新启动应用程序,通过手动输入方法登录并重新添加该功能后立即发生。
似乎auth函数尽管显示用户已登录,但并不总是如此,因此数据库查询不起作用。
但是,我通过将数据库读写规则更改为true来测试它,允许访问而不进行身份验证,但它仍然停留在“导入...”,尽管这样做。
我希望这不会太混乱,如果您需要任何其他信息来了解我的问题,请告诉我。
答案 0 :(得分: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
}
}
我希望我能提供帮助。