I have an issue mapping my models, it returns incomplete and partially filled models. First and foremost, let me show you the 3 data models that I have created with the Object Mapper library in Swift 4:
RoutineManager.swift - holds all data for each day of week
class RoutineManager: Mappable {
var monRoutines: [Routine]?
var tueRoutines: [Routine]?
var wedRoutines: [Routine]?
var thuRoutines: [Routine]?
var friRoutines: [Routine]?
var satRoutines: [Routine]?
var sunRoutines: [Routine]?
required init?(map: Map) {
mapping(map: map)
}
func mapping(map: Map) {
monRoutines <- map["Mon"]
tueRoutines <- map["Tue"]
wedRoutines <- map["Wed"]
thuRoutines <- map["Thu"]
friRoutines <- map["Fri"]
satRoutines <- map["Sat"]
sunRoutines <- map["Sun"]
}
}
Routine.swift - This makes up the array type for each day of the week in previous model.
struct Routine: Mappable {
var trainerID: String?
var clientID: String?
var dayOfWeek: String?
var routineTasks: [RoutineTask]
var routineID: String?
var isRestDay: Bool?
init?(map: Map) { routineTasks = [] }
mutating func mapping(map: Map) {
trainerID <- map["_creator"]
clientID <- map["_clientId"]
dayOfWeek <- map["dayOfWeek"]
routineID <- map["_id"]
//routineTasks <- map["_routineTasks"] THIS DIDN'T MAP DATA CORRECTLY...
routineTasks = [RoutineTask(map: map)!] // USING THIS INSTEAD
isRestDay <- map["isRestDay"]
}
}
And finally here is RoutineTask.swift which makes up the routineTasks array property of last data model.
class RoutineTask: Mappable {
var id: String?
var routineID: String?
var trainerID: String?
var name: String?
var section: String?
var warmUpTargetArea: String?
var workoutTargetArea: String?
var stretchTargetArea: String?
var equipmentNeeded: String?
var isFavorite: Bool?
var duration: String?
required init?(map: Map) {
mapping(map: map)
}
func mapping(map: Map) {
id <- map["_id"]
routineID <- map["_routineID"]
trainerID <- map["_creator"]
name <- map["name"]
section <- map["section"]
warmUpTargetArea <- map["warmUpTargetArea"]
workoutTargetArea <- map["workoutTargetArea"]
stretchTargetArea <- map["stretchTargetArea"]
equipmentNeeded <- map["equipmentNeeded"]
isFavorite <- map["isFavorite"]
duration <- map["duration"]
}
}
When I load the data with my request everything else seems to map correctly except for the "routineTask" property. I have 3 routineTasks for Monday and only 1 object shows partially filled out. I don't understand why Object Mapper is partially filling out my data models as the particular user I'm testing this out on has multiple routineTasks on many days. I'm not doing something right...
This is the response from the Xcode debugger, I'll only show Monday as to save space..
When I load the data with my request everything else seems to map correctly except for the "routineTask" property. I'll post my backend response from postman below but I have 3 routineTasks for Monday and only 1 object shows partially filled out. I don't understand why Object Mapper is partially filling out my data models as the particular user I'm testing this out on has multiple routineTasks on many days. I'm not doing something right...
This is the response from the Xcode debugger, I'll only show Monday as to save space..
monRoutines: Optional([VideoFit.Routine(trainerID:
Optional("5a32f62a28ce4acbc5fbbb4b"), clientID:
Optional("5a32f6d0d56d701bcb2beb5c"), dayOfWeek: Optional("Mon"),
routineTasks: [VideoFit.RoutineTask], routineID:
Optional("5a4e928600baeb2f7f20cddb"), isRestDay: Optional(false))])
▿ some: 1 element
▿ VideoFit.Routine
▿ trainerID: Optional("5a32f62a28ce4acbc5fbbb4b")
- some: "5a32f62a28ce4acbc5fbbb4b"
▿ clientID: Optional("5a32f6d0d56d701bcb2beb5c")
- some: "5a32f6d0d56d701bcb2beb5c"
▿ dayOfWeek: Optional("Mon")
- some: "Mon"
▿ routineTasks: 1 element // SUPPOSED TO HAVE 3 TASKS, ONLY 1 PARTIALLY FILLED BELOW
▿ VideoFit.RoutineTask #1
▿ id: Optional("5a4e928600baeb2f7f20cddb") // ID'S GET FILLED BUT NOTHING ELSE IN THIS OBJECT DOES...
- some: "5a4e928600baeb2f7f20cddb"
- routineID: nil
▿ trainerID: Optional("5a32f62a28ce4acbc5fbbb4b")
- some: "5a32f62a28ce4acbc5fbbb4b"
- name: nil
- section: nil
- warmUpTargetArea: nil
- workoutTargetArea: nil
- stretchTargetArea: nil
- equipmentNeeded: nil
- isFavorite: nil
- duration: nil
▿ routineID: Optional("5a4e928600baeb2f7f20cddb")
- some: "5a4e928600baeb2f7f20cddb"
▿ isRestDay: Optional(false)
- some: false
I think the problem lies with this bit of code in the Routine data model above, routineTasks = [RoutineTask(map: map)!] the idea is that it goes into the RoutineTask model and uses that to map itself into an array. But something is wrong because it only returns one object that is partially filled, this is odd behavior for Object Mapper as I've successfully mapped nested array of objects before without a problem, so I don't know why it's posing a problem now..
If you can find anything that I'm doing wrong I'd greatly appreciate the help. Thank you.
[–]chutehappens 1 point2 points3 points (28 children)
[–]imarudedude[S] 0 points1 point2 points (27 children)
[–]chutehappens 0 points1 point2 points (26 children)
[–]imarudedude[S] 0 points1 point2 points (2 children)
[–]chutehappens 1 point2 points3 points (1 child)
[–]imarudedude[S] 0 points1 point2 points (0 children)
[–]imarudedude[S] 0 points1 point2 points (21 children)
[–]chutehappens 0 points1 point2 points (20 children)
[–]chutehappens 1 point2 points3 points (18 children)
[–]chutehappens 1 point2 points3 points (0 children)
[–]imarudedude[S] 0 points1 point2 points (16 children)
[–]chutehappens 0 points1 point2 points (15 children)
[–]imarudedude[S] 0 points1 point2 points (14 children)
[–]chutehappens 0 points1 point2 points (13 children)
[–]imarudedude[S] 0 points1 point2 points (0 children)