简体   繁体   中英

Alamofire multiple request

I have tableView and I use Alamofire to send and get request from server. as you can see in picture, in my tableView I send multiple request in table's header and cell at index 0,1. if I send my request in one function it will too long to get response and can't control error for each request. what is best way to handle this problem?

thanks

You should create one request .

So by parsing your request you should set up your data source into an array , so each row will get the data from a single item of that array.

Then you would update your table view with this data source.

For instance:

1) Create a variable for data source (outside of methods):

var source : [Something]

2) Request all in a single request

Alamofire.request(_,_,_)
.responseJSON {
   // create a data source
   // this request should be a merge of your three request stated in your question
   // source can be set as [Something] for instance
}

3) In your cellForRow delegate method you should do something like this:

func tableView(_ tableView: UITableView, 
  cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
   let cell = tableView.dequeueReusableCell( ... 
   dataForARow = self.source[indexPath.row]
   cell.label.text = dataForARow["name"] //etc ...
}

Multiple requests is fine just display your table cell OR header based on the response of each request.

Until you didn't receive response for any request set height to 0 for that particular cell or header.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM