2

I am trying to create multiple children inside a child. I can currently create this inside 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 class of the recipe 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 I now want to create another array of children which would be inside "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 have looked at 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 better questions or examples out there.

8
  • Show use the full structure inside which parent you have added this name and id Commented May 11, 2017 at 14:33
  • @NiravD Please see my updated structure Commented May 11, 2017 at 14:41
  • Are you having that recipe Id where you want to add this array? Commented May 11, 2017 at 14:48
  • No, the recipe ID is already being added to the recipe. Commented May 11, 2017 at 14:51
  • What I'm saying is how do you know in which recipe you need to add this methods now by updating it or there is only one recipe ? Commented May 11, 2017 at 14:52

1 Answer 1

4

You can create multiple children inside of a node just as you have been doing with the "recipe" node. For example:

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

        }
     }
   }
 }

This is better practice as you can look up each step by key. Although, as Firebase Database keys are strings ordered lexicographically, it would be better practice to use .childByAutoId() for each step, to ensure all the steps come in order and are unique. I'll just keep using "stepn" for this example though.

If you needed further information inside each step, just make one of the step keys a parent node again:

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

        }
     }
   }
 }

This can be achieved by by calling .set() on a Dictionary of Dictionaries. For example:

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

myDatabaseReference.set(dict)
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks Bradley, this makes much more sense as you mention it'll allow me to refer to it using the key in the future. But how would I add all of this at once? I am trying to do all of this within one action.
You can call .set() on a Dictionary of Dictionaries and it'll just work. For example: ["Method":["Step1":["SpatulaRequired":true,"temp":400],["Step2":["SpatulaRequired":false,"temp":500]]]
@BradleyMackey Good answer and a couple of things. Please add the code in your comment to your answer as it will be easier to read - also make sure it's Swift to match the tag in the OP. Also, naming the nodes within method as you suggest may be an issue as step1 will be followed by step10, not step2. You may want to use childByAutoId to generate the parent keys for those nodes as that will order them correctly (or use a numeric integer index as a child (step: 1, step: 2, step: 3) etc).You may want to denormalize this structure as well as it's a little deep.
Thank you for this answer. It is much clearer, I have been able to create my database in the same structure you have provided where the method gets its own unique ID. However how do I refer to the methods ID. At the moment I can only refer to the recipe ID. Sorry if this is off topic.
@BradleyMackey thank you so much for your advice, you've been very helpful!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.