iOS swift UITableView ReloadData 性能

iOS swift UITableView ReloadData performance

我的 ReloadData 调用在实际 ReloadData 调用和第一次调用 tableview 的 cellForRowAtIndexPath 函数之间花费了 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()
}

视图模型代码:

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 实例 class

let iobj_NearbyLocationsVM: NearbyLocationsViewModel = NearbyLocationsViewModel()

然后在 viewDidLoad 中我将 ViewModel 的委托设置为 self 如下:

iobj_NearbyLocationsVM.iobj_LocationAPICompleteDelegate = self

然后当数据全部加载到 ViewModel 中时调用委托函数,使用以下调用 ReloadPage

func LocationAPIComplete()
{
    reloadPage()
}

如果有人想查看任何其他代码,请告诉我。在此先感谢您的帮助。

所以我对 milo526 的回答做了一些调查,发现按如下方式更改对我的委托的调用似乎已经解决了问题。

            //Switch back to main thread
            dispatch_async(dispatch_get_main_queue()) { ()
                self.iobj_LocationAPICompleteDelegate?.LocationAPIComplete()
            }

我想我现在的重要评论/声明是——没有人应该 post 使用 NSURLSession.sharedSession() 和 dataTaskWithURL 的示例,而不解决闭包中所需的线程切换。 :)