Firebase唯一标识符(用户id)无法识别,并且在尝试访问数据库时应用程序崩溃?Swift iOS9
在我的应用程序中,我下载并安装了POD,包括Firebase/FirebaseAuth/FirebaseDatabase/FirebaseStorage/etc,在我的AppDelegate中使用了FIRApp.configure()等。我成功地登录和注销了用户,并能够上传/下载数据。Firebase突然无法识别我的用户UUID。我不知道为什么,但它只是停止了 一旦应用程序加载,一切正常(仍然没有UUID,但应用程序实际加载),但如果我试图进入一个必须连接到Firebase才能获取上传/下载数据的屏幕,它就会崩溃。它发生在每个需要访问Firebase的屏幕上,而不是其他屏幕。我在下面添加了一个ColorController视图控制器来显示崩溃发生的线路 在我的AppDelegate“didFinishLaunchingWithOptions”中,我添加了一些代码,以检查应用程序是否连接到Firebase,并记录为“成功连接”。但是,我还添加了一些代码来检查print语句中的UUID,它返回nil 有什么想法吗Firebase唯一标识符(用户id)无法识别,并且在尝试访问数据库时应用程序崩溃?Swift iOS9,ios,swift,firebase,firebase-realtime-database,firebase-authentication,Ios,Swift,Firebase,Firebase Realtime Database,Firebase Authentication,在我的应用程序中,我下载并安装了POD,包括Firebase/FirebaseAuth/FirebaseDatabase/FirebaseStorage/etc,在我的AppDelegate中使用了FIRApp.configure()等。我成功地登录和注销了用户,并能够上传/下载数据。Firebase突然无法识别我的用户UUID。我不知道为什么,但它只是停止了 一旦应用程序加载,一切正常(仍然没有UUID,但应用程序实际加载),但如果我试图进入一个必须连接到Firebase才能获取上传/下载数据
import UIKit
import Firebase
import FirebaseAuth
import FirebaseDatabase
import FirebaseStorage
import GoogleMaps
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
override init() {
super.init()
//Configure App for FireBase
FIRApp.configure()
//Firebase app Offline Updates
FIRDatabase.database().persistenceEnabled = true
GMSServices.provideAPIKey("myAPIKey")
}//end override init
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//Make sure the app is connected to Firebase
let connectedRef = FIRDatabase.database().referenceWithPath(".info/connected")
connectedRef.observeEventType(.Value, withBlock: {
(connected) in
let currentUserID = FIRAuth.auth()?.currentUser?.uid
if let boolean = connected.value as? Bool where boolean == true {
//This line successfully prints
print("\nApp Delegate: Firebase is successfully connected\n")
print("\nApp Delegate: UUID is \(currentUserID)\n")
} else {
print("\nApp Delegate: Firebase is NOT connected\n")
}
})
if currentUserID != nil{
print("\nApp Delegate: UUID is \(currentUserID)\n")
} else{
//These 2 lines print and UUID prints as "nil"
print("\nApp Delegate: No User ID Available\n")
print("\nApp Delegate: UUID is \(currentUserID)\n")
}
}
下面是一个视图控制器,它可以访问Firebase,但会崩溃
import UIKit
import Firebase
import FirebaseAuth
import FirebaseDatabase
class ColorController: UIViewController {
@IBOutlet weak var colorLabel: UILabel!
var dbRef: FIRDatabaseReference!
let userID = FIRAuth.auth()?.currentUser?.uid
override func viewDidLoad() {
super.viewDidLoad()
//I tried both of these Separately. I commented out the one I didn't use
self.dbRef = FIRDatabase.database().referenceFromURL("https://myApp.firebaseio.com")
//I also tried switching to
self.dbRef = FIRDatabase.database().reference()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(true)
let usersRef = self.dbRef.child("users")//it crashes on this line
let userIDRef = usersRef.child(userID!)
let colorData = userIDRef.child("colorData")
colorData.observeEventType(.Value, withBlock: {
(snapshot) in
if let dict = snapshot.value as? [String:AnyObject]{
let chosenColor = dict["chosenColor"] as? String
self.colorLabel = chosenColor!
}
})
}
}
这是来自ColorController的崩溃
//this is from line 'let usersRef = self.dbRef.child("users")'
Thread1: EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, subcode=0x0)
换衣服
let usersRef = self.dbRef.child("users")
至
let usersRef = FIRDatabase.database().reference().child("users")
let userIDRef = usersRef.child(FIRAuth!.auth()!.currentUser!.uid)
let colorData = userIDRef.child("colorData")
{
"rules": {
".read": true,
".write": true
}
}
如果在访问数据库中的节点之前未对用户进行身份验证,则需要更改安全规则:-
发件人:-
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
至
let usersRef = FIRDatabase.database().reference().child("users")
let userIDRef = usersRef.child(FIRAuth!.auth()!.currentUser!.uid)
let colorData = userIDRef.child("colorData")
{
"rules": {
".read": true,
".write": true
}
}
问题是注册vc中的错误,我没有将其包括在本页中@贾斯汀·多恩明白了。在注册vc时,我将类的一个属性设置为
//SignUpController Property
let userID = FIRAuth.auth()?.currentUser?.uid
关于我的签名按钮行动
FIRAuth.auth()?.createUserWithEmail(self.emailTextField.text!, password: self.passwordTextField.text!, completion: {
(user, error) in
使用此方法创建新帐户时,user(from user,error)是新创建的用户,其uid可通过user访问!uid。userID属性和此uid有2个不同的值。要修复它,我必须从类的顶部除去属性,并将其移动到FIRAuth.auth()?.createUserWithEmail完成块中,如下所示:
FIRAuth.auth()?.createUserWithEmail(self.emailTextField.text!, password: self.passwordTextField.text!, completion: {
(user, error) in
//The property should be removed from the top of the class to here
let userID = FIRAuth.auth()?.currentUser?.uid
//This should always print a successful match
if user!.uid != currentUserID{
print("\nYOUR USER ID'S DON'T MATCH")
}else{
print("\nTHE USER ID'S SUCCESSFULLY MATCH")
}
注册控制器代码、问题和答案如下:
希望这对别人有帮助 当您说“UUID”时,您似乎指的是用户的UID(每个Firebase身份验证用户的唯一标识符)。我花了一段时间才弄明白,所以你可能想更新你的问题以使用正确的术语。不确定什么会导致崩溃。。。如果你能得到任何额外的细节(如崩溃的堆栈跟踪)并将其添加到问题中,可能会有所帮助。@Frank van Puffelen我现在就更新这个缩写。我使用uuid作为唯一用户标识符的缩写。@Michael Zehenbauer我稍后会添加堆栈跟踪。谢谢你的建议看看这个答案的警告部分:-这是你试图用当前用户ID做的事情…啊,我想我终于找到问题了。当应用程序首次启动时,FireAuth向您发送一个uid(#1)。当用户使用FirAuth.createAccount创建帐户时。。。通过电子邮件,我得到了一个不同的uid(#2)。#1uid是数据库中显示的内容。#2 uid是Auth中的内容。基本上我有两个不同的uid。Firebase不知道我是谁。稍后我将把代码作为一个新问题发布。这是一个奇怪的问题。在这里我解释了这个问题。这个问题都是由我的CreateCount vc造成的。贾斯汀·多恩指出了这一点。在注册类的顶部,我将属性设置为let usersRef=FIRDatabase.database().reference().child(“用户”)。我把它移到FIRAuth.auth()?.createUserWithEmail的完成块中,然后一切都从那里开始了。我真的很感谢你的帮助。希望我能在1天内回报你:)