我已经实现了以下代码,它工作正常,它显示正确的数据,刷新时刷新。但是当快速向垃圾邮件发送垃圾邮件几次时,它最终会导致致命的错误 - 索引超出范围。
这让我发疯,有人能看出我的代码出了什么问题吗?
from StringIO import StringIO
import csv
res_io = StringIO()
header= ["review","id"]
csv_writer = csv.DictWriter(
res_io, fieldnames=header
#delimiter=str(',')#, quoting=csv.QUOTE_NONNUMERIC
)
csv_writer.writerows([ {k:str(i[k]).replace("u'","'")}if type(i[k])==dict else {k:i[k]} for i in results for k in i ])
答案 0 :(得分:1)
如果能解决这个问题,你能试试吗?因为这似乎是它崩溃的地方。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell
if nameArray.count > indexPath.row && addressArray.count > indexPath.row {
cell.projectDesc.text = nameArray[indexPath.row].uppercased()
cell.projectAddress.text = addressArray[indexPath.row]
}
return cell
}
添加if refresher.isRefreshing
将阻止多次调用此数据请求。
func downloadJsonWithURL() {
// If already refreshing don't run the code below
if refresher.isRefreshing {
// Emtpy arrays to avoid index out of range crash
nameArray.removeAll(keepingCapacity: false)
addressArray.removeAll(keepingCapacity: false)
let url = NSURL(string: urlString)
URLSession.shared.dataTask(with: (url as URL?)!, completionHandler: {(data, response, error) -> Void in
if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary {
if let projectArray = jsonObj!.value(forKey: "project") as? NSArray {
for project in projectArray{
if let projectDict = project as? NSDictionary {
if let name = projectDict.value(forKey: "societe") {
self.nameArray.append(name as! String)
}
}
if let projectDict = project as? NSDictionary {
if let name = projectDict.value(forKey: "address") {
self.addressArray.append(name as! String)
}
}
}
}
OperationQueue.main.addOperation(
{
self.tableView.reloadData()
self.refresher.endRefreshing()
})
}
}).resume()
}
}
答案 1 :(得分:1)
问题是您在接收响应之前是从数组中删除对象。它创建了一个场景,其中numberOfRowsInSection
返回不同的值,并且在调用cellForRowAtIndexPath
时,它在数组中有不同的(零)项,由于索引超出范围问题导致应用程序崩溃。这通常是多线程的问题。
要解决此问题,请仅在成功从API调用接收数据时清空阵列。为此,您需要在for project in projectArray{
之上添加removeAll元素的代码。
使您的代码如下:
override func viewDidLoad()
{
super.viewDidLoad()
nameArray.removeAll(keepingCapacity: false)
addressArray.removeAll(keepingCapacity: false)
refresher = UIRefreshControl()
refresher.attributedTitle = NSAttributedString(string: "Reloading")
refresher.addTarget(self, action: #selector(downloadJsonWithURL), for: UIControlEvents.valueChanged)
tableView.addSubview(refresher)
self.downloadJsonWithURL()
}
func downloadJsonWithURL()
{
let url = NSURL(string: urlString)
URLSession.shared.dataTask(with: (url as URL?)!, completionHandler: {(data, response, error) -> Void in
if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary {
if let projectArray = jsonObj!.value(forKey: "project") as? NSArray {
nameArray.removeAll(keepingCapacity: false)
addressArray.removeAll(keepingCapacity: false)
for project in projectArray{
if let projectDict = project as? NSDictionary {
if let name = projectDict.value(forKey: "societe") {
self.nameArray.append(name as! String)
}
}
if let projectDict = project as? NSDictionary {
if let name = projectDict.value(forKey: "address") {
self.addressArray.append(name as! String)
}
}
}
}
OperationQueue.main.addOperation(
{
self.tableView.reloadData()
self.refresher.endRefreshing()
})
}
}).resume()
}