![](/img/trans.png)
[英]Swift iOS 9 UITableView reloadData after CoreData finished
[英]iOS swift UITableView ReloadData performance
从实际的ReloadData调用到第一次调用tableview的cellForRowAtIndexPath函数之间,我的ReloadData调用花费了40秒钟以上。 有谁知道为什么会有这样的表现受到打击?
这是供您参考的代码:在我的webAPI数据返回并完全加载到适当的本地对象中之后,将调用此代码。
func reloadPage()
{
btnMenu.hidden = false
btnMapAll.hidden = false
if ApplicationMode != AppMode.Normal
{
btnUseGPS.hidden = false
}
else
{
btnUseGPS.hidden = true
}
StartTime = CACurrentMediaTime()
self.NearbyLocationsTable.reloadData()
}
这是我的cellForRowAtIndexPath函数。 您可以看到我在第一次调用该函数的开始时就打印时间。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if EndTime == nil
{
EndTime = CACurrentMediaTime() - StartTime!
println("cellForRowAtIndexPath Time Elapsed \(EndTime?.description)")
}
var lobj_NearbyLocationEntry: NearbyLocationsCell? = tableView.dequeueReusableCellWithIdentifier(lc_LocationCellTableIdentifier) as? NearbyLocationsCell
if(lobj_NearbyLocationEntry == nil)
{
lobj_NearbyLocationEntry = NearbyLocationsCell(style: UITableViewCellStyle.Default, reuseIdentifier: lc_LocationCellTableIdentifier)
}
lobj_NearbyLocationEntry!.ConfigureCell(gobj_NearbyLocations[indexPath.row].Title, pd_Rating: gobj_NearbyLocations[indexPath.row].Rating, ps_Distance: gobj_NearbyLocations[indexPath.row].Distance, ps_Attributes: gobj_NearbyLocations[indexPath.row].AttributesTexts, pi_RowIndex: indexPath.row)
return lobj_NearbyLocationEntry!
}
打印的信息如下:cellForRowAtIndexPath经过的时间可选(“ 40.0729780000402”)
有时经过的时间只有12秒,但是仍然太长。 知道是什么原因造成的吗?
我确实实现了numberOfRowsInSection函数。 这会在调用reloadData之后的几毫秒内执行。 我还实现了willDisplayCell函数,但是只有在cellForRowAtIndexPath之后(即40秒过去之后)才调用该函数。
一个想法或建议将不胜感激。
这是其他代码:
协议/委托定义:
protocol LocationAPICallDelegate
{
func LocationAPIComplete()
}
ViewModel代码:
var iobj_LocationAPICompleteDelegate: LocationAPICallDelegate?
func NearbyLocationsGet(ps_Langauge: String, pi_Day_Of_Week: Int, pd_Latitude: Double, pd_Longitude: Double, pl_CityID: Int, pi_Distance: Int, pb_Rating_For_Men: Bool, pi_NumberOfEntries: Int, pb_Location_In_Metric: Bool)
{
var ls_CompleteURL: String = ""
var ls_NSURL: NSURL
ls_CompleteURL = BuildURL(ps_Langauge, pi_Day_Of_Week: pi_Day_Of_Week, pd_Latitude: pd_Latitude, pd_Longitude: pd_Longitude, pl_CityID: pl_CityID, pi_Distance: pi_Distance, pb_Rating_For_Men: pb_Rating_For_Men, pi_NumberOfEntries: pi_NumberOfEntries, pb_Location_In_Metric: pb_Location_In_Metric)
var request : NSMutableURLRequest = NSMutableURLRequest()
ls_NSURL = NSURL(string: ls_CompleteURL)!
let urlSession = NSURLSession.sharedSession()
let jsonQuery = urlSession.dataTaskWithURL(ls_NSURL, completionHandler: { data, response, error -> Void in
if (error != nil) {
println(error.localizedDescription)
}
var err: NSError?
var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as
[[String:AnyObject]]
if (err != nil) {
println("JSON Error \(err!.localizedDescription)")
}
else
{
self.LoadJSONLocationDataToObjects(jsonResult)
self.iobj_LocationAPICompleteDelegate?.LocationAPIComplete()
}
})
jsonQuery.resume()
}
您可以看到在下面的ViewController代码中调用了该委托。
class NearbyLocationsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, LocationAPICallDelegate, DOWAPICallDelegate, SideBarDelegate, GPSLocationDelegate
然后我声明一个ViewModel类的实例
let iobj_NearbyLocationsVM: NearbyLocationsViewModel = NearbyLocationsViewModel()
然后在viewDidLoad中,将ViewModel的委托设置为self,如下所示:
iobj_NearbyLocationsVM.iobj_LocationAPICompleteDelegate = self
然后,使用以下调用ReloadPage的以下命令将数据全部加载到ViewModel中时,将调用委托函数:
func LocationAPIComplete()
{
reloadPage()
}
如果有人想查看其他任何代码,请告诉我。 在此先感谢您的协助。
因此,我对milo526的答案进行了一些调查,发现如下更改对我的代表的呼叫似乎已解决了该问题。
//Switch back to main thread
dispatch_async(dispatch_get_main_queue()) { ()
self.iobj_LocationAPICompleteDelegate?.LocationAPIComplete()
}
我想我现在的主要评论/声明是-没有人发布过将NSURLSession.sharedSession()与dataTaskWithURL一起使用的示例,而没有解决闭包中所需的线程切换问题。 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.