0

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
}
Misha
  • 128
  • 1
  • 12
  • 1
    Rather than simply `true` or `false` you have to return `locList` in the `completion` block. Basically a function containing an asynchronous task cannot have a return value. – vadian Sep 16 '17 at 19:17
  • Ok so I've changed it to func getLocations(completion: @escaping (_ response: [LocationList]) -> ()) -> [LocationList] { ... } ? – Misha Sep 16 '17 at 19:29
  • Yes, but forget `_ response:` It has no effect in Swift 3 – vadian Sep 16 '17 at 19:31
  • I'm doing it in swift 4 and its complaining that it needs a parameter. So for the closure part if I change it to the above it doesn't seem to do anything anymore. – Misha Sep 16 '17 at 19:40
  • 1
    I mean `func getLocations(completion: @escaping ([LocationList]) -> ())` (no return value) this is supposed to work also in Swift 4. And of course you have to write `completion(locList)` and specify an appropriate value on failure. – vadian Sep 16 '17 at 19:47
  • Ha, yeah thats what I had and it wasn't working... Well I hadn't noticed the VPN on my windows got disconnected and thats why nothing was happening. lol – Misha Sep 16 '17 at 20:04

0 Answers0