0

I try to populate my TableView with information that i get from a JSON But already on numberOfRowsInSection it does not work

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        let getVideoCount = Alamofire.request(.GET, APICall).responseJSON { (responseData) -> Void in
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                let swiftyJsonVar = JSON(responseData.result.value!)

                if let resData = swiftyJsonVar["items"].arrayObject {
                    self.arrRes = resData as! [[String:AnyObject]]
                }
                print(self.arrRes.count) //runs after
            })
        }

        getVideoCount.resume()
        print(self.arrRes.count) //runs first
        return arrRes.count

    }

i try with the dispatch_async to run the JSON call before return the value, but still it print the //runs first before the //runs after

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Fabian Boulegue
  • 6,476
  • 14
  • 47
  • 72
  • You can't do this in `numberOfRowsInSecfion` you should do it in a method such as `viewDidLoad` and call `reloadData` on the tableview in the completion closure – Paulw11 Nov 28 '15 at 11:00
  • @Paulw11 could you explain a bit more on this "reloadData" – Fabian Boulegue Nov 28 '15 at 11:00
  • Refer to the tableview documentation and the table view programming guide. – Paulw11 Nov 28 '15 at 11:02
  • From the Alamofire readme: (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 Nov 28 '15 at 11:02
  • @MartinR ok this seem to be really completed for a starter :-) thanks anyway :( – Fabian Boulegue Nov 28 '15 at 11:03
  • @FabianBoulegue: The point is that the request is handled asynchronously and there is no way to get the response synchronously in the numberOfRowsInSection method. Another good explanation here: http://stackoverflow.com/questions/25901460/alamofire-get-api-request-not-working-as-expected. This might also help: http://stackoverflow.com/questions/32457385/tableview-reloaddata-is-not-working-swift – Martin R Nov 28 '15 at 11:11
  • @MartinR ah ok, thanks. I now tried it with "self.tableView.reloadData()" but the tableview seem to update it self all the time if i scroll is this correct? – Fabian Boulegue Nov 28 '15 at 11:20
  • As @Paulw11 said above, you must move the request from numberOfRowsInSection to viewDidLoad so that it is done only once. – Martin R Nov 28 '15 at 11:24
  • @MartinR ah ok, well then i need to check how to move the "updated" values for the return of numberOfRowsInSection from my viewDidLoad into numberOfRowsInSection again – Fabian Boulegue Nov 28 '15 at 11:26
  • The result from the request is already stored in `self.arrRes`! numberOfRowsInSection just needs to return `self.arrRes.count`. – Martin R Nov 28 '15 at 11:28

0 Answers0