0
votes

I would like to save and retrieve features to and from Firebase into a TableView. The child I would like to save them under is the uid (unique user id)

so a feature would look like this in the database:

Firebase database

The ideal situation, is how the "derde" is saved, so the uid as a key and "derde" as the value.

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

where uid is a String, retrieved from authdata somewhere else in the code.

If I save it like this, it saves it to the specific uid path. If I want to add another feature by clicking on the + in the TableViewController, it saves it to the same path, so the Firebase database is updated with the new value and so instead of two features you only end up with one updated feature.

You can prevent this by working with the chilByAutoId() method, to save a list of items. The code would look like this:

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

via this way, a feature is saved, as you can see in the above image at: "vierde" This allows you to save multiple features with all the same uid, but different autoId.

But, if I save it like this, my tableView stays empty. The TableViewController is like this:

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()
    })



}

Problem lies in this code: if let postDictionary = snap.value as? String {

This conditional binding does not succeed, because the value is not a String, but the autoId key has no value, only the child under it which is the uid has a value "vierde"

Two possible solutions which I am asking you guys:

1) How can I save multiple features with the same uid without using the autoId? 2) If I am obliged to use the autoId, how can I make sure it observes the value of the uid key under the autoId, instead of the non existing value of the autoId.

Thanks for your help!

1
You've included a link to a picture of the JSON tree in your question. Please replace that with the actual JSON as text, which you can easily get by clicking the Export button in your Firebase database. Having the JSON as text makes it searchable, allows us to easily use it to test with your actual data and use it in our answer and in general is just a Good Thing to do.Frank van Puffelen

1 Answers

1
votes

I think the answer to the question is to build a dictionary out of the key:value pairs of data and store that as a child of your uid node

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

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

ref.setValue(featureDict)

results in

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

The limitation here is the key's names, and then the ability to add even more data about each feature.

Here's a potentially better option

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"

The auto_id_x is generated by autoId and allows you to add however many features you want, change their names and summaries. etc. The children of each auto_id_x are (or could be) stored in a dictionary and saved per the above example.