如何将此对象模型数据保存到Firebase

时间:2016-11-22 15:40:40

标签: ios swift firebase firebase-realtime-database

我有一个如下例程的模型:

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

还有UITableView我在创建该模型的新实例之前收集数据,该模型曾经创建过如下所示:

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

如何将其保存到Firebase中以使其适合我的数据结构

"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"
        }
      }
    }
  }
}

user unique identifier将是当前登录的用户uid,routine unique identifier将是autoId,但我如何通过模型将数据推送到Firebase与模型相同?

1 个答案:

答案 0 :(得分:1)

如果您的用户已通过身份验证,请尝试: -

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"
            ]]
        ])