rob rob - 1 year ago 211
iOS Question

Pull to refresh and Alamofire using Swift 3

I am using Alamofire to get data from a web url (JSON). I am trying to implement pull to refresh into my project. I have done it but don't know if it is correct or if the data is getting updated when refreshed. my code is:

var refresh = UIRefreshControl()

refresh.addTarget(self, action: #selector(self.refreshData), for: UIControlEvents.valueChanged)

func refreshData() {

Alamofire.request("").responseJSON(completionHandler: {
response in
self.tableView.separatorStyle = UITableViewCellSeparatorStyle.singleLine




Is this correct?

Answer Source

You are doing correctly but you need reload tableView and stop UIRefreshControl on main thread.

DispatchQueue.main.async {

Note: Instead of setting separatorStyle always on API request you need to set it once with viewDidLoad or with Interface builder.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download