我已使用This链接在我的代码中调用API请求。像下面一样
SpeedyNetworking.getUrl(url: url,isSetHeader: Constants.isTokenReqired) {[weak self] (response) in
guard let strongSelf = self else {
return
}
if response.success {
if response.statusCode == 200{
// var model : [FeedNewModel]?
if let model = response.result(model: [FeedNewModel].self){
strongSelf.isDataLoading = false
if model.count > 0{
strongSelf.resCount = model.count
strongSelf.feedModel.append(contentsOf : model)
strongSelf.feedviewModel = strongSelf.feedModel.map({
return try! FeedNewViewModel(model: $0, Delegate: strongSelf.delegate)})
}
DispatchQueue.main.async {
//Reload with delay
strongSelf.stopActivityIndicator()
strongSelf.refreshControl.endRefreshing()
strongSelf.tableView.reloadData()
}
}
}else{
strongSelf.showAlert(title: Constants.SERVER_ERROR, msg: Constants.EXECPTION)
strongSelf.stopActivityIndicator()
}
}else{
strongSelf.stopActivityIndicator()
}
}
对于第一个请求,代码工作正常。当我尝试对UITableView
进行分页时,UI卡住了。我在这里使用了MVVM体系结构,并将feedviewModel
附加到UITableView
上。通过UITableView
中的这个,我还从url下载图像。为此,我引用以下代码
class ImageLoader: UIImageView {
var imageURL: URL?
let activityIndicator = UIActivityIndicatorView()
func loadImageWithUrl(_ url: URL) {
// setup activityIndicator...
activityIndicator.color = .darkGray
addSubview(activityIndicator)
activityIndicator.translatesAutoresizingMaskIntoConstraints = false
activityIndicator.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
activityIndicator.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
imageURL = url
image = nil
activityIndicator.startAnimating()
// retrieves image if already available in cache
if let imageFromCache = imageCache.object(forKey: url as AnyObject) as? UIImage {
self.image = imageFromCache
activityIndicator.stopAnimating()
return
}
DispatchQueue.global(qos: .userInitiated).async { // image does not available in cache.. so retrieving it from url...
URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) in
if error != nil {
print(error as Any)
DispatchQueue.main.async(execute: {
self.activityIndicator.stopAnimating()
})
return
}
//
DispatchQueue.main.async(execute: {
if let unwrappedData = data, let imageToCache = UIImage(data: unwrappedData) {
if self.imageURL == url {
self.image = imageToCache
}
imageCache.setObject(imageToCache, forKey: url as AnyObject)
}
self.activityIndicator.stopAnimating()
})
}).resume()
}
}
}
分页代码如下
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
//scroll dragging
spinner.stopAnimating()
if ((tableView.contentOffset.y + tableView.frame.size.height) >= tableView.contentSize.height)
{
if !isDataLoading!{
isDataLoading = true
self.pageNo = self.pageNo + 1
if self.resCount == Int(self.pagecount!){
self.fetchdataNew(page: String(describing: self.pageNo))
}else{
let view = "No More Feeds"
let label = UILabel()
label.text = view
label.frame = CGRect(x: CGFloat(0), y: CGFloat(0), width: tableView.bounds.width, height: CGFloat(44))
label.textAlignment = .center
self.tableView.tableFooterView = label
self.tableView.tableFooterView?.isHidden = false
}
}
}
}
但是当我们对UITableView
进行分页时,UI仍然卡住/冻结。