Asif mimi
Asif mimi

Reputation: 113

Swift Table View taking too much time to load JSON data

There are total number of 114 data to fetch from web through API request. But it takes 10-15 seconds to load the data. I tried DispatchQueue() but found no improvement. ViewDidLoad and JSON parse code:

func parseJSON()  {
    let url = URL(string: "https://api.alquran.cloud/v1/quran/ar.alafasy")
    guard url != nil else{
        print("URL Founr Nill")
        return
    }
    URLSession.shared.dataTask(with: url!) { (data, response, error) in
        if error == nil && data != nil{
            do{
                let response = try JSONDecoder().decode(Surah.self, from: data!)
                self.surahName = response.data.surahs
                DispatchQueue.main.async {
                    self.tableView.reloadData()
                    self.tableView.tableFooterView = UIView(frame: .zero)
                }
            }catch{
                print(error)
            }
        }
    }.resume() 
}

Table View code:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell:QuranAudioCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! QuranAudioCell
    let surah = surahName[indexPath.row]
    cell.nameLbl.text = surah.englishName
    cell.arabicNameLbl.text = surah.name
    return cell
}

Upvotes: 0

Views: 541

Answers (1)

Daij-Djan
Daij-Djan

Reputation: 50089

Based on the code above the only thing that can be sooo slow is the networr request you are making.

Your DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0) { in viewDidLoad is rather pointless TBH

The only way to speed this up at this point is debugging the network call.

  • Is your Connection generally slow? (Try to ping google.com in terminal)
  • Is your Connection to the specific server slow? (Try to ping the host in terminal)
  • Is your Server slow to respond (Try to open it in Webbrowser or use curl in terminal to get the JSON

Upvotes: 1

Related Questions