更新 tableview 导致滞后和警告
Updating tableview causes lag and warning
我有一个数组,其中包含从提取请求中检索到的数据。在我的 table 视图上方,我有一个按钮可以启动另一个带有过滤器的获取请求。每当我点击按钮时,我都会清空包含上一个获取请求的文件的数组,这也是我用来引用 numberOfRowInSection
的同一个数组。然后用来自新请求的数据填充该数组。这行得通,但是在我按下按钮和 table 视图重新加载新数据之间存在巨大的滞后。
然后我收到警告 "This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes. This will cause an exception in a future release."
我在另一个 post 中读到我需要实施 dispatch_async
,但我不确定在哪里:
dispatch_async(dispatch_get_main_queue(), {
// code here
})
剩余代码:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.recordings.count
}
@IBAction func publicButtonPressed(sender: AnyObject) {
self.recordings.removeAll()
let task = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
do {
let dict = try NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves) as? NSDictionary
self.fetchedArray = dict!["recordings"] as? NSArray
print("fetched array count \(self.fetchedArray!.count)")
if let jsonArray: NSArray = self.fetchedArray {
for var i=0; i<jsonArray.count; ++i {
let dictResult = jsonArray.objectAtIndex(i) as! NSDictionary
let recording = Recordings()
recording.trackURL = dictResult["url"] as? String
self.recordings.append(recording)
}
}
else
{
// No error thrown, but no NSDictionary
}
self.audioTable.reloadData()
}
catch let parseError {
// Log the error thrown by `JSONObjectWithData`
}
})
task.resume()
}
与UI有关的任何事情都需要在主线程上进行。在您的代码中,这将是 reloadData
调用。因此,修改您的代码以将 reloadData
包装在 dispatch_async
中
dispatch_async(dispatch_get_main_queue(), {
self.audioTable.reloadData()
})
我有一个数组,其中包含从提取请求中检索到的数据。在我的 table 视图上方,我有一个按钮可以启动另一个带有过滤器的获取请求。每当我点击按钮时,我都会清空包含上一个获取请求的文件的数组,这也是我用来引用 numberOfRowInSection
的同一个数组。然后用来自新请求的数据填充该数组。这行得通,但是在我按下按钮和 table 视图重新加载新数据之间存在巨大的滞后。
然后我收到警告 "This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes. This will cause an exception in a future release."
我在另一个 post 中读到我需要实施 dispatch_async
,但我不确定在哪里:
dispatch_async(dispatch_get_main_queue(), {
// code here
})
剩余代码:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.recordings.count
}
@IBAction func publicButtonPressed(sender: AnyObject) {
self.recordings.removeAll()
let task = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
do {
let dict = try NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves) as? NSDictionary
self.fetchedArray = dict!["recordings"] as? NSArray
print("fetched array count \(self.fetchedArray!.count)")
if let jsonArray: NSArray = self.fetchedArray {
for var i=0; i<jsonArray.count; ++i {
let dictResult = jsonArray.objectAtIndex(i) as! NSDictionary
let recording = Recordings()
recording.trackURL = dictResult["url"] as? String
self.recordings.append(recording)
}
}
else
{
// No error thrown, but no NSDictionary
}
self.audioTable.reloadData()
}
catch let parseError {
// Log the error thrown by `JSONObjectWithData`
}
})
task.resume()
}
与UI有关的任何事情都需要在主线程上进行。在您的代码中,这将是 reloadData
调用。因此,修改您的代码以将 reloadData
包装在 dispatch_async
dispatch_async(dispatch_get_main_queue(), {
self.audioTable.reloadData()
})