0

I'm working with alamofire and I make a request (getPrescriptions) when users open the app which is a block with a completionHandler. That function retrieves a list from ws and can be paginated, but I don't know how many items I have at the endpoint.. So I do something like:

 //todo: temp? pagination
        if prescriptions.count == 10 {
            getPrescriptions(page: page!+1, completionHandler: { (_) in
                    print("get prescription page", page!+1)
                completionHandler(prescriptions)
            })
        } else {
            completionHandler(prescriptions)
        }

I've made that temporary, but I can't figure out how could I do this better. The flow is like that: session.getPrescriptions(completion) and getPrescriptions can call himself if needed.

here is the full code:

 static func getPrescriptions(page: Int? = 1, completionHandler: @escaping (_ prescriptions: [Prescription]?) -> Void){

    let endPoint = "/prescriptions?page=\(page!)&size=10"

    doDefaultRequestWith(endPoint: endPoint, requestType: .get, completionWithError: { (error) in
        return completionHandler(nil)

    }, completionWithResponse: { (response) in

        guard let prescriptionsArray = response.result.value as? NSArray else {
            return
        }
        var prescriptions: [Prescription] = []
        for prescriptionJSON in prescriptionsArray {
            if let json = prescriptionJSON as? [String: AnyObject] {
                if let prescription = Prescription(JSON: json) {
                    if let address = prescription.address {
                        session.setAddress(address)
                    }
                    prescriptions.append(prescription)
                }
            }
        }

        if prescriptions.count > 0 {
            session.setPrescriptions(prescriptions: prescriptions)
        } else {
            completionHandler(nil)
        }

        //todo: temp? pagination
        if prescriptions.count == 10 {
            getPrescriptions(page: page!+1, completionHandler: { (_) in
                    print("get prescription page", page!+1)
                completionHandler(prescriptions)
            })
        } else {
            completionHandler(prescriptions)
        }

    }, completionWithJSON: { (_) in

    })


}
Gehlen
  • 160
  • 1
  • 15
  • Your code is a little messy, no offence - its hard to decifer which variables get captured by which closures - and it will get harder as the time goes by. If you can, you should rather add a field in the response inidicating either the total count of prescriptions or if there is another page to be retrieved. – Losiowaty May 02 '17 at 23:23
  • @Losiowaty, its fine (is really a messy) The thing is that I cant change ws, so I count the items if there is 10, I just do another request (from himself) to check page 2.. But I think on third page (it didn't happened yet), my first call will not receive the completion.. Anyway, its a messy and ugly. I have to make that better, otherwise - how can I sleep tonight? Thanks for the feedback! – Gehlen May 03 '17 at 14:07

0 Answers0