0

I use this piece of code but the reload table always seems to load directly after the alamofire request instead of filling the data first. The braces are put in the correct way but it still loads Step 3 before Step 2.

Output: - The filter list is not empty filling data according to filters. - step 1 - step 3 - step 2: found 35 houses for Haarlem - step 2: found 100 houses for Amsterdam

println("The filter list is not empty filling data according to filters.")
        if let castedFilters = filters as? [Filter] {
            println("step 1")
            for filter in castedFilters{
                var parameters : [String : NSObject] = ["apisleutel": "#########", "module": "Objecten", "get": "Huur", "plaats": filter.plaats, "pt": filter.maximumprijs, "pv": filter.minimumprijs, "wov": filter.oppervlakte, "ka": filter.kamers, "output": "json"]

                self.makeCall(parameters) { responseObject, error in
                    let json = JSON(responseObject!)
                    /**/
                    let count: Int? = json["Response"]["objecten"]["object"].array?.count
                    if((count) != nil)
                    {
                        println("step 2: found \(count!) houses for "+filter.plaats)

                        if let ct = count {
                            for index in 0...ct-1 {
                                //Adding house to houses array
                                var adres = json["Response"]["objecten"]["object"][index]["adres"].string
                                let newHouse = House(straat: adres!)
                                self.houses.append(newHouses)
                            }
                        }
                    }
                    else
                    {
                        let alert = UIAlertView()
                        alert.title = "Fout"
                        alert.message = "No houses found, consider changing the filters."
                        alert.addButtonWithTitle("Ok")
                        alert.show()
                    }
                    return
                }

            }
            println("step 3")
            tableView.reloadData()
        }

Call function

func makeCall(parameters: [String : NSObject], completionHandler: (responseObject: NSDictionary?, error: NSError?) -> ()) {
    var heap: NSDictionary
    Alamofire.request(.GET, "http://www.huizenzoeker.nl/api/v2/", parameters: parameters)
        .responseJSON { request, response, responseObject, error in
            completionHandler(responseObject: responseObject as? NSDictionary, error: error)
    }
}
Youri
  • 1
  • 1
  • 2
    From https://github.com/Alamofire/Alamofire (emphasis added): "Networking in Alamofire is done **asynchronously**. ... Rather than blocking execution to wait for a response from the server, a callback is specified to handle the response once it's received. The result of a request is **only available inside the scope of a response handler**. Any execution contingent on the response or data received from the server **must be done within a handler**." – Martin R Jan 05 '15 at 19:33
  • Similar question here: http://stackoverflow.com/questions/27390656/how-to-return-value-from-alamofire. – Martin R Jan 05 '15 at 19:34
  • Even if I put it in a function with completion handler its still in a loop so the call to this handler is made multiple times and still it takes step 3 first. – Youri Jan 05 '15 at 21:39

0 Answers0