O
O
Olga_082017-01-24 10:28:06
Swift
Olga_08, 2017-01-24 10:28:06

How to wait for a network request to complete and only then execute the following code?

In didSelectRow, a request is sent to the server (using Alamofire), and the text for the cell comes in the response, but what needs to be done to wait for the response and only then perform the rendering of the cell?

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let cell = tableView.cellForRow(at: indexPath) as? AMHistoryTableViewCell {
            if let selectedPath = selectedPath {
                if selectedPath.section == indexPath.section, selectedPath.row == indexPath.row {
                    self.selectedPath = nil
              }
            } else {
                self.selectedPath = indexPath
                let item = self.items[(self.tableView.indexPath(for: cell)?.row)!]
                if item.show == true {
                    let model = ReceiptRequestModel()
                    model.transactionId = item.id
                    Net.shared.request(requestType: .his(type: .receipt), model: model, completionHandler: {(error, json) in
                        if error == nil, let json = json {
                            if let resultModel = Mapper<ReceiptResponseModel>().map(JSON: json) { cell.descLabel = resultModel.desc
                            }
                        }
                    })
                }
            }
            toggleCell(cell, animated: true)
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
belenerd, 2017-03-28
@olga_08

Alamofire has a completionHandler which is designed to execute some code after a network request has been completed. Perform cell rendering in this block.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question