使用 Swift 更新 Firebase 中的子值
Posted
技术标签:
【中文标题】使用 Swift 更新 Firebase 中的子值【英文标题】:Update child values in Firebase with Swift 【发布时间】:2017-03-22 00:27:52 【问题描述】:我正在尝试在用户点击按钮后更新 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()
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?"
【问题讨论】:
您好,您解决了这个问题吗?如果可以,请问如何? 【参考方案1】:您想更改Places
的值,即child(uid)
的子级中的值。
let values = ["place": self.placeNameLabel.text]
let ref = FIRDatabase.database().reference().root.child("users").child(uid).updateChildValues(["Places": values])
【讨论】:
谢谢!这确实有助于我尝试做的一件事。虽然有一些补充......我每次用户向数组添加新位置时都尝试创建一个项目......所以不仅仅是更新相同的子值,以及只将字符串的一部分保存到数据库.在用户从 GMSAutocompleteViewController 中选择之后,我已经在帖子中添加了额外的代码。所以我只想将名称值保存到 Firebase。再次感谢! 如何在upateChildValues的回调中获取更新节点的值?【参考方案2】: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])
【讨论】:
以上是关于使用 Swift 更新 Firebase 中的子值的主要内容,如果未能解决你的问题,请参考以下文章
Swift:如何对数组中结构的子值使用 UISearchResultsUpdating?