Jee*_*eef 30 uitableview reloaddata ios swift
我相信我遇到了一个问题,我的闭包发生在后台线程上,而我的UITableView的更新速度不够快.我正在调用一个REST服务,在我的关闭中我有一个tableView.reloadData()电话,但这需要几秒钟才能完成.如何使数据重新加载速度更快(可能在主线程上?)
REST查询功能 - 使用SwiftyJSON库进行解码
func asyncFlightsQuery() {
var url : String = "http://127.0.0.1:5000/flights"
var request : NSMutableURLRequest = NSMutableURLRequest()
request.URL = NSURL(string: url)
request.HTTPMethod = "GET"
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue(), completionHandler:{ (response:NSURLResponse!, networkData: NSData!, error: NSError!) -> Void in
var error: AutoreleasingUnsafeMutablePointer<NSError?> = nil
// Parse with SwiftyJSON
let json = JSON(data: networkData)
// Empty out Results array
self.resultArray = []
// Populate Results Array
for (key: String, subJson: JSON) in json["flights"] {
print ("KEY: \(key) ")
print (subJson["flightId"])
print ("\n")
self.resultArray.append(subJson)
}
print ("Calling reloadData on table..??")
self.tableView.reloadData()
})
}
Run Code Online (Sandbox Code Playgroud)
一旦self.tableView.reloadData()在我的调试器中调用
Kir*_*ins 83
UIKit isn't thread safe. The UI should only be updated from main thread:
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}
Run Code Online (Sandbox Code Playgroud)
Update. In Swift 3 and later use:
DispatchQueue.main.async {
self.tableView.reloadData()
}
Run Code Online (Sandbox Code Playgroud)
你也可以像这样重新加载 UITableView
self.tblMainTable.performSelectorOnMainThread(Selector("reloadData"), withObject: nil, waitUntilDone: true)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
33246 次 |
| 最近记录: |