【发布时间】:2020-10-12 10:11:27
【问题描述】:
通过 API 请求从 Web 获取的数据总数为 114。但是加载数据需要 10-15 秒。我尝试了 DispatchQueue() 但没有发现任何改进。 ViewDidLoad 和 JSON 解析代码:
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()
}
表格查看代码:
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
}
【问题讨论】:
-
您正在发出网络请求。您确定不是网络请求花费了这么长时间(而不是实际的 UI 更新)?
-
其实我不确定
-
在
if error == nil && data != nil{上添加打印,检查“日期”所需的时间。可能是……如果你的连接不好,或者服务器很慢,那很正常,不是吗? -
if error == nil && data != nil{ print("Total data: (data!)") .是的,这需要很多时间