MHDev
MHDev

Reputation: 145

How can I save this object model data to Firebase

I have a model for a routine like the following:

struct Routine {
    var routineName: String!
    var routineExercisesAndSets: [String:Int]!
}

And a UITableView where I collect data before creating a new instance of that model, which once created looks like:

var routine = Routine()

//values are assigned to the routine here and then when printed looks like below

(routineName: All Over, 
routineExercisesAndSets: ["Bent Over Row": 3, "Barbell Curl": 3, "Bench Press (Flat)": 4])

How could I save this into Firebase so that it fits my data structure

"routines": {
  "users unique identifier": {
    "routine unique identifier": {
      "routine_name": "routine name",
      "routine_create_date": "routine created date",
      "exercises": {
        "exercise name": {
          "Sets": "number of sets",
          "rest between sets": "timer duration"
        }
      }
    }
  }
}

The user unique identifier will be the current logged in users uid and routine unique identifier will be an autoId, but how can I work through the model to push data to Firebase the same as the model?

Upvotes: 1

Views: 327

Answers (1)

Dravidian
Dravidian

Reputation: 9945

If your user is authenticated try this:-

FIRDatabase.database().reference().child("routines/\(FIRAuth.auth()!.currentUser!.uid)").childByAutoId().setValue([
        "routine_Name" : "All Over",
        "routine_create_date": "routine created date",
        "exercises": [
            "exercise name": [
                "Sets": "number of sets",
                "rest between sets": "timer duration"
            ]]
        ])

Upvotes: 1

Related Questions