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

时间:2019-01-18 19:25:17

标签: swift firebase

我面临的问题是,当用户首次注册我的应用程序(并且我看到在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的支持团队一起开了案。

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用 updateChildValues 代替setValue?

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

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