gpt4 book ai didi

swift - 用户首次登录时无法写入 Firebase

转载 作者:行者123 更新时间:2023-11-30 10:53:03 24 4
gpt4 key购买 nike

我面临的问题是,当用户第一次注册我的应用程序(并且我看到 Firebase 上的身份验证中添加的名称)并尝试撰写帖子时,它不允许他们,它不允许也给出一个错误。有趣的是,当用户离开应用程序、删除历史记录并重新启动应用程序时,用户可以发布帖子并写入实时数据库。做了一些测试,我看到了同样的问题,并且日志中似乎有错误:

2019-01-18 14:02:32.251806-0500 Jama3aV2[35603:8067503] [BoringSSL] nw_protocol_boringssl_get_output_frames(1301) [C17.1:2][0x10254c2e0] get output frames failed, state 8196
2019-01-18 14:02:32.252321-0500 Jama3aV2[35603:8067503] [BoringSSL] nw_protocol_boringssl_get_output_frames(1301) [C17.1:2][0x10254c2e0] get output frames failed, state 8196

我尝试了很多搜索,但找不到解决方案,有人说这可能是 iOS 12 的问题,而另一些人说这是 Xcode 的问题,所以我不确定。以下是创建帖子的代码。

    @IBAction func handlePostButton(_ sender: Any) {

guard let image = desciptionImage.image else {return}

var currentLocation: CLLocation!
if (CLLocationManager.authorizationStatus() == .authorizedWhenInUse || CLLocationManager.authorizationStatus() == .authorizedAlways){
currentLocation = locationManager.location
}

//Need to specify the area of ryerson.
// if ((currentLocation.coordinate.longitude < 43.657821 && currentLocation.coordinate.longitude > 43.656258 ) && (currentLocation.coordinate.latitude < -79.380299 && currentLocation.coordinate.latitude > -79.37842 )) {

self.uploadProfileImage(image, completion: { (url) in
guard let url = url else {return}
guard let userProfile = UserService.currentUserProfile else { return }
var postID: String!
//Lets make a reference to the database
var postref: DatabaseReference!

//Check which view its coming from
if self.cityLocationInNewPost?.school == nil {
postref = Database.database().reference().child("posts/school/\((self.school)!)").childByAutoId()
postID = "\((postref)!)"
print("The post ID \(String(describing: postID))")
postID = postID.replacingOccurrences(of: "https://jama3a-version2.firebaseio.com/posts/school/\((self.school)!)/", with: "")
print(postID!)
}else {
postref = Database.database().reference().child("posts/school/\((self.cityLocationInNewPost?.school)!)").childByAutoId()

}
guard let uid = Auth.auth().currentUser?.uid else { return }
self.ref = Database.database().reference().child("users/profile/\(uid)/numberOfPosts")
self.ref.observeSingleEvent(of: .value) { (snapshot) in
if let number = snapshot.value as? Int {
self.numberOfPosts = number
}
}

//This will assign this post a specific identifier that we can reference it by.
//This is referencing the place where you write a post
let postObject = [
"author":[
"uid": userProfile.uid,
"photoURL": userProfile.photoURL.absoluteString,
"username": userProfile.username,
"email": userProfile.email,
"numberOfPosts": 0
],
"text": self.textView.text,
"postphotoURL": url,
"Status": "\(self.statusOfPost)",
"timestamp": [".sv": "timestamp"],
"location":[
"latitude": currentLocation.coordinate.latitude,
"logitude": currentLocation.coordinate.longitude
],
"PostID": postID
] as [String:Any]

postref.setValue(postObject, withCompletionBlock: {Error, ref in
if Error == nil {

//This will set the value
DispatchQueue.main.asyncAfter(deadline: .now()){
if self.cityLocationInNewPost?.school == nil {
//This is coming from the subscription need to initialte that view
let Storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = Storyboard.instantiateViewController(withIdentifier: "LoggedIn2ViewController") as! LoggedIn2ViewController
vc.school = self.school
self.navigationController?.pushViewController(vc, animated: true)

}else {
self.dismiss(animated: true, completion: nil)
}
//lets set the numberofposts to the variable
self.updateNumberOfPosts()
}
}else {
print(Error?.localizedDescription as Any)
self.creatAlert(title: "Error", message: "Error making a Post!")
}
})
})
//Giving an alert that the post was succesful
let alert = UIAlertController(title: "Notification", message: "Your post is successful! Thank you for the contribution", preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Awesome!", style: UIAlertAction.Style.default, handler: nil))
self.present(alert, animated: true, completion: nil)
// }else {
// self.creatAlert(title: "Cant Make Post!", message: "Cant make post since you are not on the School's campus!")
// textView.text = ""
// }
}

以及以下内容来上传个人资料图片:

func updateNumberOfPosts() {
//We will need to increment the number of posts
guard let uid = Auth.auth().currentUser?.uid else { return }
ref = Database.database().reference().child("users/profile/\(uid)/numberOfPosts")
ref.observeSingleEvent(of: .value) { (snapshot) in
if var number = snapshot.value as? Int {
print(number)
number = number + 1
self.ref.setValue(number)
print(number)
}
}

同样,错误仅在用户首次注册并尝试发帖时发生。这影响了我的生产;我也向 Firebase 的支持团队立案。

最佳答案

您是否尝试过使用updateChildValues而不是setValue?

使用setValue会覆盖指定位置的数据,包括任何子节点。另一方面,updateChildValues 允许您通过指定键的路径来更新较低级别的子值。

https://firebase.google.com/docs/database/ios/read-and-write

关于swift - 用户首次登录时无法写入 Firebase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54260280/

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