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

时间:2017-09-06 17:29:35

标签: ios swift firebase firebase-realtime-database

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

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

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

创建帐户代码:

@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和Email Auth:

@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类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 个答案:

答案 0 :(得分:1)

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