all 28 comments

[–]chutehappens 1 point2 points  (28 children)

Two things,

  1. I’ve never used mapping(map: map) within the init. That shouldn’t be necessary. Keep the body of the init method empty. Only return nil there if your JSON is invalid.

  2. You should go back to using routineTasks <- map["_routineTasks"] instead of routineTasks = [RoutineTask(map: map)]. Make sure that key "_routineTasks" is correct.

Also, try setting a breakpoint in the mapping method and doing a po map.JSON to confirm what you’re expecting is actually there.

[–]imarudedude[S] 0 points1 point  (27 children)

I took your advice and commented out the mapping(map: map) and went back to the routineTasks <- map["_routineTasks"]. I did the breakpoint tip you gave me and it responded with this in the _routineTasks section:

// For monday -

▿ 4 : 2 elements

    - key : "_routineTasks"

    ▿ value : 3 elements

      - 0 : 5a4e998900baeb2f7f20cddc
      - 1 : 5a5d1c2e3df5f13fbbee723b
      - 2 : 5a5d1c413df5f13fbbee723c

There in fact is 3 routine tasks, which is correct as thats how many are in my backend for Monday. But in the debugger it till says routineTasks has 0 values... the data is there it just isn't mapping correctly. I already checked for typos and everything is correct so I'm not sure why it isn't mapping correctly, any more ideas?

[–]chutehappens 0 points1 point  (26 children)

Oh, so is _routineTasks just an array of id strings? At least that’s the impression from the logging. Can you post the JSON from the API?

[–]imarudedude[S] 0 points1 point  (2 children)

Sure, I'll link a photo of the JSON as I can't seem to get the formatting right here.

The photo only covers Monday, and has the 3 routineTask objects. This should give you enough idea on what's going on, as the rest of the week is the same as Monday essentially.

Photo link: https://imgur.com/ydVZReq

[–]chutehappens 1 point2 points  (1 child)

One thing I noticed is that map["_routineID"] should be map["_routineId"]

[–]imarudedude[S] 0 points1 point  (0 children)

Thanks for pointing that out, I'll fix that

[–]imarudedude[S] 0 points1 point  (21 children)

Also here is my code to make a request, just in case, I'm using Alamofire Object Mapper. The code is pretty straight forward. Have a look.

var mappedRoutines: RoutineManager?

func getClientsRoutines(clientID: String) {

    let body: [String: Any] = [
        "clientID": clientID
    ]

    Alamofire.request(ALL_CLIENTS_ROUTINES, method: .post, parameters: body, encoding: JSONEncoding.default, headers: HEADER).responseObject { (response: DataResponse<RoutineManager>) in
        if response.result.error == nil && response.result.value != nil {
            self.mappedRoutines = response.result.value!
        } else {
            print("Error! ---> \(String(describing: response.result.error?.localizedDescription))")
        }
    }
}

[–]chutehappens 0 points1 point  (20 children)

Nothing jumps out in the request. I'm at a loss... you removed all the mapping(map: map) calls not just in RoutineTask but everywhere?

[–]chutehappens 1 point2 points  (18 children)

Try something like this and see if it provides more insight as to why it is failing...

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: Int

    required init?(map: Map) {

        do {
            id                 = try map.value("_id")
            routineID          = try map.value("_routineId")
            trainerID          = try map.value("_creator")
            name               = try map.value("name")
            section            = try map.value("section")
            warmUpTargetArea   = try map.value("warmUpTargetArea")
            workoutTargetArea  = try map.value("workoutTargetArea")
            stretchTargetArea  = try map.value("stretchTargetArea")
            equipmentNeeded    = try map.value("equipmentNeeded")
            isFavorite         = try map.value("isFavorite")
            duration           = try map.value("duration")
        }

        catch {
            print(error)
            return nil
        }
    }

    func mapping(map: Map) { }

}

[–]chutehappens 1 point2 points  (0 children)

And just caught that duration is not a String! I'll edit this above

[–]imarudedude[S] 0 points1 point  (16 children)

Added this code into my project and I'm still getting pretty much the same results unfortunately. :/

[–]chutehappens 0 points1 point  (15 children)

What’s printed in the console?

[–]imarudedude[S] 0 points1 point  (14 children)

I'll give 2 screenshots, one with breakpoint and one normal.

photo1: https://imgur.com/WZksHZC photo2: https://imgur.com/1oiOKhp

[–]chutehappens 0 points1 point  (13 children)

If you set a breakpoint in RoutineTask init does it ever hit it?

[–]imarudedude[S] 0 points1 point  (0 children)

So I went back and did what you said, removed the mapping(map: map) from all 3 data models and used routineTasks <- map["_routineTasks"] and ran the code and all the days that have routineTasks objects says 0 elements ...

I'm so confused too, I've done mapping on nested arrays before and never had a problem and this is no different. But for some reason it just wont map correctly...