ios - UITableView willDisplay 在调用 API 分页时不断调用自身,问题在于异步 API 调用
问题描述
我正在使用 MVVM 并且这个 tableview 委托在 ViewController 问题是因为 API 调用函数使用 ViewModel 对象
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.row == photosVM.albumData.photos!.photo!.count - 1 {
if photosVM.albumData.photos!.pages! >= photosVM.albumData.photos!.page! {
spinner.color = UIColor.FlickrAlbum_theme
spinner = UIActivityIndicatorView(style: .medium)
spinner.startAnimating()
spinner.frame = CGRect(x: CGFloat(0), y: CGFloat(0), width: tableView.bounds.width, height: CGFloat(44))
tblPhotos.tableFooterView = spinner
tblPhotos.tableFooterView?.isHidden = false
//Calling search API with search keywords
photosVM.getPhotos(pageNumber: photosVM.albumData.photos!.page! + 1, searchText: txtSearch.text!) { [self] (status) in
lblDescription.text = txtSearch.text
if status == "success" {
lblDescription.text = txtSearch.text
tblPhotos.reloadData()
}
else {
//showing error message
showAlert(message: status)
}
}
spinner.stopAnimating()
tblPhotos.tableFooterView = nil
tblPhotos.tableFooterView?.isHidden = true
}
}
这是我的 ViewModel,其中我有一个调用 API 并将数据绑定到视图控制器的功能
func getPhotos(pageNumber: Int = 1, completion: @escaping (String) -> Void) {
let params = GetPhotosBody()
params.format = "json"
params.apiKey = Constants.apiKey
params.nojsoncallback = 1
params.page = pageNumber
params.text = ""
params.contentType = 1
ServerManager.getAllPhotos(params: params) { [self] (status, data) in
if status == "success" {
if pageNumber == 1 {
albumData = data
}
else {
albumData.photos?.page = data?.photos?.page!
albumData.photos?.photo?.append(contentsOf: (data!.photos!.photo!))
}
//Cache Data
DataCache.instance.write(object: albumData.toJSON() as NSCoding, forKey: CacheValue.photos)
}
if let photos = DataCache.instance.readObject(forKey: CacheValue.photos) {
albumData = Mapper<FlickrAlbumPhotosResponse>().map(JSONObject: photos)
}
else {
albumData = nil
}
completion(status)
}
}
这是 API 调用管理器,我正在使用 completionblock 来处理异步方法
public static func getAllPhotos(params: GetPhotosBody, completion: @escaping (String ,FlickrAlbumPhotosResponse?) -> Void) {
//creating URL
//addingPercentEncoding is used for encoding the url string to support text with spaces
let url = "\(Constants.baseURL)\(FlickrAlbumEndpoints.getPhotos.rawValue)&format=\(String(describing: params.format!))&nojsoncallback=\(String(describing: params.nojsoncallback!))&api_key=\(String(describing: params.apiKey!))&page=\(String(describing: params.page!))".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
//creating request
var request = URLRequest(url: URL(string: url)!)
request.httpMethod = HTTPMethod.get.rawValue
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
//Making request to the server to call the API
Alamofire.request(request).responseObject { (response: DataResponse<FlickrAlbumPhotosResponse>) in
//Check the response of the API
switch response.result {
case .success:
let data = response.result.value
completion("success",data)
case .failure(let error):
completion(error.localizedDescription, nil)
}
}
}
每当我关闭互联网时,它都会一次又一次地显示错误消息,因为它一直在调用将显示委托。如果我以错误的方式使用 MVVM,请告诉我并提出您的建议。谢谢
解决方案
我强烈怀疑当 api 调用失败(因为没有互联网)时,您正在为视图模型数组分配一些缓存数据。分配它会触发您在视图控制器中的绑定功能,该功能会重新加载 tableview,因此 api 调用会再次进入无限循环。
从您的 api 函数中删除缓存分配,它应该可以正常工作。
推荐阅读
- angular - Angular / Ionic 中的 Observables
- android - Android 发布的应用程序无法访问互联网 Xamarin
- c# - 不能在接口的实现中使用更通用的属性类型?
- database - Spark MapWithState 管理会话状态
- java - android opencv 3.4.1 构建命令失败。未定义的引用
- c++ - 功能在特定时间内消耗墙壁时间
- c# - WPF组合框不以双向更新
- python-3.x - 网页抓取:迭代网站的页面,而无需使用 Python 和请求编辑 url
- python - Choropleth 地图没有为国家着色?
- python - 使用 QComboBox 选择显示的不同小部件集