gpt4 book ai didi

ios - Firebase:虽然我正在设置数据,但值并未在 Firebase 的实时数据库中实时反射(reflect)

转载 作者:行者123 更新时间:2023-11-30 12:39:20 25 4
gpt4 key购买 nike

应用程序配置已完成。

数据库规则设置为

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


//declaration
var userDB: FIRDatabaseReference!

在viewDidLoad()中

userDB = FIRDatabase.database().reference()
//for reference database

操作方法如下

@IBAction func next(_ sender: Any) {
guard fullname.text != "",email.text != "",password.text != "" ,confirmPassword.text != "" else {
return
}
if(password.text == confirmPassword.text)
{
FIRAuth.auth()?.createUser(withEmail: email.text!, password: password.text!, completion: { (user, error) in
if let error = error {
print(error.localizedDescription )
}
if let error = error {
print(error.localizedDescription)
}

if let user = user {
print(user.uid)
let changeRequest = FIRAuth.auth()!.currentUser!.profileChangeRequest()
changeRequest.displayName = self.fullname.text!
changeRequest.commitChanges(completion: nil)

let imageRef = self.userStorage.child("\(user.uid).jpg")

let data = UIImageJPEGRepresentation(self.uploadPictureImageview.image!, 0.5)

let uploadTask = imageRef.put(data!, metadata: nil, completion: { (metadata, err) in
if err != nil {
print(err!.localizedDescription)
}

imageRef.downloadURL(completion: { (url, er) in
if er != nil {
print(er!.localizedDescription)
}

print(url)
if let url = url {
print(url)
let userInfo: [String : String] = ["uid" : user.uid,
"full name" : self.fullname.text!,
"urlToImage" : url.absoluteString]

self.userDB.child("users").child(user.uid).setValue(userInfo)


let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "usersVC")

self.present(vc, animated: true, completion: nil)

}

})

})

uploadTask.resume()

}

})
}
else{
print("Password didnot match")
}
}

字典类型已从 [String: Any] 更改为 [String : String]

数据未保存在 Firebase 的实时数据库中。我是不是忘记了什么?

最佳答案

打开与 Firebase 引用的连接的正确方法如下...

 //declare the reference as an implicitly unwrapped optional
var userDB: FIRDatabaseReference!

//set the value of the reference in viewDidLoad
override func viewDidLoad(){
userDB = FIRDatabase().database().reference()
}

如果您在执行上面所示的操作后仍然遇到相同的问题,那么您可能忘记从 Firebase 项目下载 GoogleService-info.plist 文件并将其添加到您的 Xcode 项目

还要确保 self.fullname.text! 有一个值,否则它将无法工作,因为 Firebase 不会采用 nil

编辑

还在您的 AppDelegate.swift 文件中进行检查以确保调用 FIRApp.configure() 。我相信你可以称之为 didFinishLaunchingWithOptions 或者你可以做..

override init(){
FIRApp.configure()
}

关于ios - Firebase:虽然我正在设置数据,但值并未在 Firebase 的实时数据库中实时反射(reflect),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42437010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com