2017-03-22 48 views
1

我試圖在用戶點擊按鈕時實現更新Firebase數據庫。當用戶登錄時(在我的情況下用Facebook),數據結構已成功創建,並且數據樹中的初始值已創建。使用Swift更新Firebase中的子值

我想要完成的是一旦用戶在應用程序中,他們創建一個新的項目將其保存到數據庫,因此更新下一個已創建的子值的值。查看我的代碼和截圖以供參考 - 感謝您的幫助!

// 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?" 
     } 
    } 

回答

3

你想改變的Places的價值,這是在child(uid)孩子的價值。

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

謝謝!這有助於我嘗試的一件事。添加一些但是...我試圖創建一個項目,每當用戶添加一個新的地方數組...所以不只是更新相同的子值,以及只保存一部分字符串到數據庫。我在帖子中添加了額外的代碼,用於在用戶從GMSAutocompleteViewController ....選擇後傳遞的內容,所以我只想將名稱值保存到Firebase。再次感謝! – user3708224

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]) 

如果你想要的是什麼成分Date對象更多的控制試試這個:

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])