尝试访问数据库时,Firebase唯一标识符(用户ID)无法识别且应用崩溃? Swift iOS9

时间:2016-09-12 12:35:05

标签: ios swift firebase firebase-realtime-database firebase-authentication

在我的应用中,我下载并安装了包含Firebase / FirebaseAuth / FirebaseDatabase / FirebaseStorage /等的pod,在我的AppDelegate等中使用了FIRApp.configure()。我成功登录和退出用户并能够上传/下载数据。 Firebase突然无法识别我的用户UUID。我不知道为什么,但它刚刚停止。

一旦应用程序加载一切正常(仍然没有UUID但应用程序实际加载)但如果我尝试转到一个必须连接到Firebase以获取上传/下载数据的屏幕,它就会崩溃。它发生在需要访问Firebase而不是其他屏幕的每个屏幕上。我在下面添加了一个ColorController视图控制器,以显示发生崩溃的行。

在我的AppDelegate中,'didFinishLaunchingWithOptions'我添加了一些代码来检查应用程序是否已连接到Firebase,并记录为“已成功连接”。但是我还添加了一些代码来检查print语句中的UUID,它返回nil。

有什么想法吗?

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)

2 个答案:

答案 0 :(得分:1)

只需更改

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": "auth != null",
      ".write": "auth != null"

    }
  }

  {
   "rules": {
     ".read": true,
     ".write": true
   }
 }

答案 1 :(得分:0)

问题出在SignUp vc中我没有在此页面中包含。 @Justin Doan想通了。在SignUp vc上,我已经将类的一个属性设置为

//SignUpController Property
let userID = FIRAuth.auth()?.currentUser?.uid

在signUpButton上我有

的动作
FIRAuth.auth()?.createUserWithEmail(self.emailTextField.text!, password: self.passwordTextField.text!, completion: {

            (user, error) in

使用此方法创建新帐户时,用户(来自用户,错误)是新创建的用户,您可以通过用户访问uid!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")
            }

SignUpController代码,问​​题和答案在这里:Firebase issuing out 2 different uid's -Swift iOS

希望这有助于某人!