Adding multiple children to Firebase database with Swift

I am trying to create multiple children inside a child. I can create this in my recipe:

{
  "RecipeData": {
    "recipe": {
      "-KjTSH4uPQ152Cr-hDok": {
        "name": "Cook rice",
        "ID": "-KjTSH4uPQ152Cr-hDok",
      }
    }
  }
}

      

Using:

let recipe: [String : Any] = ["name" : self.recipe.name,
                              "ID" : self.recipe.key]

      

The recipe class looks like this:

class Recipe {

    var name: String!
    var key: String

    init(from snapshot: FIRDataSnapshot) {

        let snapshotValue = snapshot.value as! [String: Any]

        self.name = snapshotValue["name"] as! String
        self.key = snapshot.key  
    }
}

      

But now I want to create another array of children, which will be inside the "method" and look something like this.

{
  "RecipeData": {
    "recipe": {
      "-KjTSH4uPQ152Cr-hDok": {
        "name": "Cook rice",
        "ID": "-KjTSH4uPQ152Cr-hDok",
        "method": [
          {
            "step 1": "instruction 1"
          },
          {
            "step 2": "instruction 2"
          },
          {
            "step 3": "instruction 3"
          }
        ]
      }
    }
  }
}

      

Edit:

The recipe is updated this way

databaseRef.child("RecipeData").child("recipe").updateChildValues(recipe)

      

I've reviewed Firebase How to update multiple children? which is written in javascript but not sure how to implement it. Feel free to let me know if there are more interesting questions or examples.

+3


source to share


1 answer


You can create multiple children inside a node just like you did with "recipe"

node. For example:

{
  "RecipeData": {
    "recipe": {
      "-KjTSH4uPQ152Cr-hDok": {
        "name": "Cook rice",
        "ID": "-KjTSH4uPQ152Cr-hDok",
        "method": {
          "Step1":"instruction1",
          "Step2":"instruction2",
          "Step3":"instruction3"

        }
     }
   }
 }

      

This is better because you can search each step by key. Although, since Firebase database keys are lexicographically ordered , it would be better to use .childByAutoId()

for each step to ensure that all steps come in order and are unique. However, I'll just use "stepn" for this example.

If you need more information at each step, just make one of the step keys the parent node again:



{
  "RecipeData": {
    "recipe": {
      "-KjTSH4uPQ152Cr-hDok": {
        "name": "Cook rice",
        "ID": "-KjTSH4uPQ152Cr-hDok",
        "method": {
          "Step1": {
             "SpatulaRequired" : true,
             "Temp" : 400
          }

        }
     }
   }
 }

      

This can be achieved by calling .set()

up dictionaries in the dictionary. For example:

let dict: [String:AnyObject] = ["Method":
                                ["Step1":
                                 ["SpatulaRequired":true,
                                             "temp":400],
                                ["Ste‌​p2":
                                 ["SpatulaRequire‌​d":false,
                                             "temp":500]‌​
                                ]]

myDatabaseReference.set(dict)

      

+4


source







All Articles