iOS应用程序在Firebase的Databse()。reference()上崩溃

时间:2021-11-26 16:58:55

This issue started suddenly. It had been working fine for the last couple of days authenticating with Facebook accounts. Today I tried implementing the Auth.auth().createUser(email: ... password:... completion:...) and suddenly my app started crashing right after authentication in function func completeSignIn(id:..., userData:...) at DataService.ds.createFirebaseDBUser(uid: usr.uid, userData: ["provider": "Email"]), it crashes in the Firebase Singleton at the line Database().reference(). I'm very lost. I checked out a branch where it used to work, but now it doesn't work either, so I created a new app in Firebase, but I'm getting the same issue. I've seen this question in a couple of places, but no answer. It's throwing the error:

这个问题突然开始了。在过去的几天里,它一直在使用Facebook帐户进行身份验证。今天我尝试实现Auth.auth()。createUser(email:... password:... completion:...)突然我的应用程序在函数func completeSignIn(id:...,userData)中认证后立即崩溃:...)在DataService.ds.createFirebaseDBUser(uid:usr.uid,userData:[“provider”:“Email”]),它在Firebase Singleton的Database()。reference()行崩溃。我很失落。我检查了它以前工作的分支,但现在它也不起作用,所以我在Firebase中创建了一个新的应用程序,但我遇到了同样的问题。我在几个地方看过这个问题,但没有答案。它抛出错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: key cannot be nil'

but I've checked the id and the dictionary that I'm passing and the values are there. Any help is appreciated.

但我已经检查了我传递的id和字典,并且值存在。任何帮助表示赞赏。

Create Account code:

创建帐户代码:

@IBAction func createAccount(_ sender: Any) {
    if let email = emailField.text, let pwd = pwdField.text{
        Auth.auth().createUser(withEmail: email, password: pwd, completion: { (user, error) in
            if error != nil {
                print("Unable to Authenticate E-mail with Firebase - Error: \(String(describing: error))")
            }else{
                print("Successfully Authenticated E-mail with Firebase")
                if let usr = user {
                    let keychainResult = KeychainWrapper.standard.set(usr.uid, forKey: KEY_UID)
                    print("Data saved to Keychain: \(keychainResult)")
                    DataService.ds.createFirebaseDBUser(uid: usr.uid, userData: ["provider": "Email"])
                }
            }
        })
    }
}

FBLogin and Email Auth:

FBLogin和电子邮件验证:

@IBAction func facebookLogin(_ sender: Any) {
    print("Start Login Process")
    let fbLogin = FBSDKLoginManager()
    print("Set Read Permissions")
    //TODO: add Read Permissions
    fbLogin.logIn(withReadPermissions: ["email","user_location"], from: self) { (result, error) in
        if error != nil{
            print("Unable to provide Authentication with Facebook - \(String(describing: error))")
        }else if result?.isCancelled == true{
            print("User Cancelled FB Authentication")
        }else {
            print("User Successfully Logged In")
            let credential = FacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)
            self.firebaseAuth(credential)
        }
    }

}

@IBAction func emailLogin(_ sender: Any) {
    if let email = emailField.text, let password = passwordField.text{
        Auth.auth().signIn(withEmail: email, password: password, completion: { (user, error) in
            if error != nil{
                print("Unable to Authenticate Firebase - \(String(describing: error))")
            } else {
                print("Succesfully authenticated with Firebase")
                if let user = user{
                    let userData = ["provider": "email"]
                    self.completeSignIn(id: user.uid, userData: userData)
                }
            }

        })
    }
}


func firebaseAuth(_ credential: AuthCredential){
    Auth.auth().signIn(with: credential, completion: { (user, error) in
        if error == nil {
            print("Succesfully authenticated with Firebase")
            if let user = user{
                let userData = ["provider": credential.provider]
                self.completeSignIn(id: user.uid, userData: userData)
            }
        } else{
            print("Unable to Authenticate Firebase - \(String(describing: error))")
        }
    })
}


func completeSignIn(id: String, userData: Dictionary<String, String>){
    DataService.ds.createFirebaseDBUser(uid: id, userData: userData)
    let keychainResult = KeychainWrapper.standard.set(id, forKey: KEY_UID)
    print("Data saved to Keychain: \(keychainResult)")
    performSegue(withIdentifier: "goToMain", sender: nil)
}

Firebase Singleton class DataService:

Firebase Singleton类DataService:

import Foundation
import Firebase

let DB_BASE = Database().reference()

class DataService {

static let ds = DataService()

private(set) var REF_BASE = DB_BASE
private(set) var REF_PROMOS = DB_BASE.child("promos")
private(set) var REF_NEGOCIOS = DB_BASE.child("negocios")
private(set) var REF_USERS = DB_BASE.child("users")

func createFirebaseDBUser(uid: String, userData: Dictionary<String, String>){
    REF_USERS.child(uid).updateChildValues(userData)
}

}

1 个解决方案

#1


1  

You just need to get a reference to your existing database rather than trying to initialize a new one. Change Database().reference() to Database.database().reference().

您只需要获取对现有数据库的引用,而不是尝试初始化新数据库。将Database()。reference()更改为Database.database()。reference()。

#1


1  

You just need to get a reference to your existing database rather than trying to initialize a new one. Change Database().reference() to Database.database().reference().

您只需要获取对现有数据库的引用,而不是尝试初始化新数据库。将Database()。reference()更改为Database.database()。reference()。