I'm having issues with loading my data into the table. On the first click nothing happens, it is hitting my getLocations function and it's completing since it prints(2) yet nothing shows up in the table. If I press back and try again the data is there. I've tried putting the DispatchQueue.main.async { reload table } in multiple areas and yet it still happens. Could it be how I'm calling the endpoint? I made a post last night about it, it's here and its working now. It was a silly over looked json structure :-x Swift 4 decoding json using Codable. Is there a way to prefetch the data?
protocol LocationListDelegate: NSObjectProtocol
{
func sendLocationList(_ controller: LocationViewController, data:
[LocationList])
}
class LocationViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var locList: [LocationList] = [] //could this be giving me problems?
@IBOutlet weak var locationListTableView: UITableView!
weak var delegate: LocationListDelegate?
override func viewDidLoad() {
super.viewDidLoad()
locationListTableView.dataSource = self
locationListTableView.delegate = self
// call endpoint
locList = getLocationsFromService()
// DispatchQueue.main.async {
// self.locationListTableView.reloadData()
// }
}
//override func viewDidAppear(_ animated: Bool) {
// super.viewDidAppear(animated)
// self.locationListTableView.reloadData()
//}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return locList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = locationListTableView.dequeueReusableCell(withIdentifier: "LocationCell") as? LocationsViewCell {
let location = locList[indexPath.row]
//print(location)
cell.updateView(locations: location)
// DispatchQueue.main.async {
// self.locationListTableView.reloadData()
// }
return cell
} else {
return LocationsViewCell()
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let location = locList[indexPath.row]
if let locDelegate = self.delegate {
locDelegate.sendLocationList(self, data: [location])
print(location)
self.dismiss(animated: true, completion: nil)
}
}
func getLocationsFromService() -> [LocationList]{
print("1")
return LocationService.instance.getLocations { (success) in
if success {
print("2")
// DispatchQueue.main.async {
// self.locationListTableView.reloadData()
// }
}
}
}
}
updated endpoint call from my previous thread which is in another swift services file
func getLocations(completion: @escaping CompletionHandler) -> [LocationList] {
let headers: HTTPHeaders = [:]
Alamofire.request(GlobalConstants.APIURL + "/getlocationlist",
method: .get, parameters: nil, encoding: JSONEncoding.default,
headers: headers).responseJSON(completionHandler: { (response) in
if response.result.error == nil {
guard let data = response.data else { return }
do {
let locations = try
JSONDecoder().decode(Locations.self, from: data)
self.locList = locations.data.LocationList
//print(locList)
completion(true)
} catch let error {
print(error)
completion(false)
}
}
else {
completion(false)
debugPrint(response.result.error as Any)
}
})
return locList
}