Lance Samaria Lance Samaria - 1 year ago 105
Swift Question

Firebase Unique Identifier (user id) not being recognized and app crashes when trying to access Database? Swift iOS9

In my app I downloaded and installed the pods including Firebase/FirebaseAuth/FirebaseDatabase/FirebaseStorage/etc, used FIRApp.configure() in my AppDelegate, etc. I successfully logged users in and out and was able to upload/download data. All of a sudden Firebase isn't recognizing my user UUID. I'm not sure why but it just stopped.

Once the app loads everything is fine (still no UUID but the app actually loads) but if I try to go to a screen that would have to connect to Firebase to get upload/download data it crashes. It happens on every screen that needs to access Firebase but not the other ones. I added a ColorController view controller below to show the line the crash happens on.

In my AppDelegate 'didFinishLaunchingWithOptions' I added some code to check wether the app is connected to Firebase and it logs as "successfully connected". However I also added some code to check for the UUID in a print statement and it returns nil.

Any ideas?

import UIKit
import Firebase
import FirebaseAuth
import FirebaseDatabase
import FirebaseStorage
import GoogleMaps

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

override init() {

//Configure App for FireBase

//Firebase app Offline Updates
FIRDatabase.database().persistenceEnabled = true

}//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")

Here is one of the view controllers that has access to Firebase but crashes

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() {

//I tried both of these Separately. I commented out the one I didn't use
self.dbRef = FIRDatabase.database().referenceFromURL("")

//I also tried switching to
self.dbRef = FIRDatabase.database().reference()

override func viewWillAppear(animated: Bool) {

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!

Here is the crash from the ColorController

//this is from line 'let usersRef = self.dbRef.child("users")'
Thread1: EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, subcode=0x0)

Answer Source

The problem was the in the SignUp vc in which I didn't include on this page. @Justin Doan figured it out. On the SignUp vc I had set the property to

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

On the signUpButton action I had

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

            (user, error) in

When you create a new account using this method, user (from user, error) is the newly created user with a uid which you can access via user!uid. The userID property and this uid have 2 different values. To fix it I had to get rid of the property from the top of the class and move it within the FIRAuth.auth()?.createUserWithEmail completion block like so:

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")
                print("\nTHE USER ID'S SUCCESSFULLY MATCH")

The SignUpController code, question, and answer is here: Firebase issuing out 2 different uid's -Swift iOS

Hope this helps someone!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download