使用Swift更新Firebase中的子值

时间:2017-03-22 00:27:53

标签: ios swift firebase firebase-realtime-database

我尝试在用户点击按钮后实施更新Firebase数据库。当用户登录时(在我的情况下使用Facebook),可以使用创建的数据树中的初始值成功创建数据结构。

我想要完成的是一旦用户进入应用程序并且他们创建了一个新项目,它将其保存到数据库,因此更新已创建的子值之一下的值。请参阅我的代码和屏幕截图以供参考 - 感谢您的帮助!

// user taps button to send item to be updated in Firebase data tree
func confirmAddPlace() {

    // add place to tableview array
    let accessToken = FBSDKAccessToken.current()
    guard let accessTokenString = accessToken?.tokenString else { return }

    let credentials = FIRFacebookAuthProvider.credential(withAccessToken: accessTokenString)
    FIRAuth.auth()?.signIn(with: credentials, completion: { (user, error) in
        if error != nil {
            print("Something went wrong with our FB user: ", error ?? "")
            return
        }

    guard let uid = user?.uid else {
        return
    }
    // here is where i am having issues
    let ref = FIRDatabase.database().reference().root.child("Users").child(uid).child("Places")
    let values = ["place": self.placeNameLabel.text]
    ref.updateChildValues(values)
})

    animateOut()
}

enter image description here

func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
        let placeID = place.placeID
        placesClient.lookUpPlaceID(placeID, callback: { (place, error) -> Void in
            if let error = error {
                print("lookup place id query error: \(error.localizedDescription)")
                return
            }
            guard let place = place else {
                return
            }
        })
        let selectedPlace = place.formattedAddress
        if let name = selectedPlace as String!
        {
            self.placeNameLabel.text = "Are you sure you would like to add \(name) to your places?"
        }
    }

2 个答案:

答案 0 :(得分:7)

您想要更改Places的值,这是child(uid)子项中的值。

let values = ["place": self.placeNameLabel.text]
let ref = FIRDatabase.database().reference().root.child("users").child(uid).updateChildValues(["Places": values])

答案 1 :(得分:0)

user3708224 - 你可以试试这个:

let values = ["place": self.placeNameLabel.text]
// create a child reference that uses a date as the key
let date = Date()
let ref = FIRDatabase.database().reference().root.child("users").child(uid).child(date).updateChildValues(["Places": values])

如果您想要更多地控制日期对象中的组件,请尝试以下方法:

let calendar = Calendar.current
let year = calendar.component(.year, from: date)

// you can do the same with [.month, .day, .hour, .minute, and more]
// This will allow you to have control of how frequently they can update the DB
// And it will allow you to sort by date

如果您想将其作为字符串上传到Firebase,请尝试:

/**
 This function returns the Date as a String
 - "Year-Month-Day"
 If the character ' / ' is used in place of ' - ' Firebase will make each component child of the previous component. 
 */
func getDate() -> String {
        let date = Date()
        let calendar = Calendar.current
        // hours + min:  -\(calendar.component(.hour, from: date))-\(calendar.component(.minute, from: date))
        return "\(calendar.component(.year, from: date))-\(calendar.component(.month, from: date))-\(calendar.component(.day, from: date))"

    }

let values = ["place": self.placeNameLabel.text]
let ref = FIRDatabase.database().reference().root.child("users").child(uid).child(getDate()).updateChildValues(["Places": values])