swift - Unusual high CPU usage in UITableView -
i'm creating exercise app, , created uitableview show of exercises available (roughly 120). , i'm getting data coredata using nsfetchedresultscontroller. problem i'm having high cpu usage when i'm scrolling through table. ranges 50%-100% depending how fast scroll.
here's cell row:
override func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell { guard let cell = tableview.dequeuereusablecell(withidentifier: "cell", for: indexpath) as? exercisecell else {fatalerror("couldn't load appropriate index path custom cell")} let exercise = self.fetchedresultscontroller.object(at: indexpath) cell.exercisetitle.text = exercise.name if let getimage = exercise.image2 { let image = uiimage(data: getimage data) cell.exerciseimage.image = image } else { cell.exerciseimage.image = nil } return cell }
i'm not sure if how i'm converting image? or how have coredata setup? awesome. thanks.
seems might doing expensive work on uiimage.
there guidelines should follow in order achieve scrolling seamlessly.
one thumb rule: try make tableview:cellforrowatindexpath
method quick possible return cell instances
- threading: try fetching or computations in background thread instead of main thread ex. here creating uiimage instances
- rendering: don’t perform data binding in
tableview:cellforrowatindexpath
, instead perform stepstableview:willdisplaycell:forrowatindexpath
- animations: eye opening core graphics performance issue. refer this: http://asyncdisplaykit.org/docs/corner-rounding.html
- caching: create own caching mechanism can effective. i.e.
- better approach access coredata result afterwards can accessible
- height calculations cells
- for dynamic height cells, don’t initialised instance of cell bound data fetch height, instead use class method returns height based on passed width , data display (adapter of cell)
Comments
Post a Comment