使用Swift在Firebase中添加值而不是更改值

时间:2016-03-18 11:20:47

标签: ios swift firebase

我想将Firebase中的功能保存和检索到TableView中。 我想保存它们的孩子是uid(唯一用户ID)

因此数据库中的功能如下所示:

Firebase database

理想的情况,就是如何保存“derde”,所以将uid作为关键词和“derde”作为价值。

@IBAction func saveButtonPressed(sender: AnyObject) {

    let featureContents = addFeatureTextField.text

    if featureContents != "" {

        // Build the new Feature.


    let newFeature: String = featureContents!

        let ref = DataService.dataService.FEATURE_REF.childByAppendingPath(uid)
        ref.setValue(newFeature)

其中uid是一个String,从代码中其他地方的authdata中检索。

如果我这样保存,它会将其保存到特定的uid路径。如果我想通过单击TableViewController中的+来添加另一个功能,它会将其保存到同一路径,因此Firebase数据库会使用新值进行更新,因此您只需使用一个更新的功能而不是两个功能。 / p>

您可以通过使用chilByAutoId()方法来防止这种情况,以保存项目列表。代码如下所示:

   @IBAction func saveButtonPressed(sender: AnyObject) {

    let featureContents = addFeatureTextField.text

    if featureContents != "" {

        // Build the new Feature.


    let newFeature: String = featureContents!

        let ref = DataService.dataService.FEATURE_REF.childByAutoId().childByAppendingPath(uid)
        ref.setValue(newFeature)

通过这种方式,可以保存一个功能,如上图所示:“vierde” 这允许您使用所有相同的uid保存多个功能,但使用不同的autoId。

但是,如果我像这样保存它,我的tableView保持为空。 TableViewController是这样的:

DataService.dataService.FEATURE_REF.observeEventType(.Value, withBlock: { snapshot in

        // The snapshot is a current look at our features data.

        print("The features in the tableView should be \(snapshot.value)")

        self.features = []

        if let snapshots = snapshot.children.allObjects as? [FDataSnapshot] {



            for snap in snapshots {

                // Make our features array for the tableView.



                if let postDictionary = snap.value as? String {



                    print("All in")


                    let key = snap.key

                    let feature = Feature(key: key, value: postDictionary)

                    // Items are returned chronologically, but it's more fun with the newest features first.

                    self.features.insert(feature, atIndex: 0)
                }
            }

        }

        // Be sure that the tableView updates when there is new data.

        self.tableView.reloadData()
    })



}

问题在于此代码:如果让postDictionary = snap.value为?字符串{

此条件绑定不成功,因为该值不是String,但autoId键没有值,只有它下面的子节点uid具有值“vierde”

我问你们两个可能的解决方案:

1)如何在不使用autoId的情况下使用相同的uid保存多个功能? 2)如果我有义务使用autoId,我怎样才能确保它在autoId下观察uid键的值,而不是autoId的非现有值。

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

我认为这个问题的答案是从密钥中构建一个字典:数据值对并存储为你的uid节点的子节点

let featureDict = [ "feature_0": "cool feature", "feature_1": "great feature"]

let ref = DataService.dataService.FEATURE_REF.childByAppendingPath(uid)

ref.setValue(featureDict)

结果

the_uid
  feature_0: "cool feature"
  feature_1: "great feature"

这里的限制是关键字的名称,然后能够添加关于每个功能的更多数据。

这是一个可能更好的选择

the_uid
  auto_id_0
    feature_name: @"cool feature"
    summary: "Everything you'd ever want to know about this feature"
  auto_id_1
    feature_name: @"great feature"
    summary: "Info about this great feature"

auto_id_x由autoId生成,允许您添加所需的许多功能,更改其名称和摘要。每个auto_id_x的子项都是(或可能)存储在字典中并按照上面的例子保存。