3

I have a very complex UIViewController that loads extremely slow sometimes, it can take 4 or 5 seconds.

I do use Core Data and I have -com.apple.CoreData.SQLDebug 1 set and have ruled out that it is the loading of my data. CoreData: annotation: total fetch execution time: 0.0166s for 314 rows.

I do have this data loaded into a UITableView but was under the impression only the first visible rows get renderend so no matter how many rows I have (< 500 currently) it should not slow the display of the UIView.

I looked at instruments and checked out Time Profiler but I did not see a way to tell which method in this UIView would be slow.

Any suggestions?

UPDATE:
For me this was caused by my custom row heights, those all have to get calculated FIRST before any data in shown:
heightForRowAtIndexPath being called for all rows & how many rows in a UITableView before performance issues?

4

1 回答 1

2

我最近有一个UITableViewController通过 Core Data 管理约 8000 行的设备。数据是用NSFetchedResultsController; 我不知道您是否正在使用NSFetchedResultsControllera NSFetchRequest,但如果您要呈现通过 Core Data 填充的表格,我强烈推荐它。

也就是说,当您创建一个NSFetchRequest以提高获取和渲染的性能时,您可以做很多调整,这就是我猜性能瓶颈所在。您可以获取成批的行,以便表格的初始呈现速度很快,并且在用户滚动表格时执行额外的提取:

[fetchRequest setFetchBatchSize:25];

我建议阅读 Apple 的性能调整和核心数据指南:

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdPerformance.html

由于您使用的是 Instruments,还请检查(根据上面的链接)Core Data 缓存未命中、获取和保存。这可能会让你知道时间花在了哪里。

您还可以尝试设置断点表方法,例如cellForRowAtIndexPath:查看加载视图时渲染的行数与用户滚动表视图时的行数。

我还设置了以下内容,这似乎可以提高我的情况下的性能:

[fetchRequest setShouldRefreshRefetchedObjects:YES];
[fetchRequest setReturnsObjectsAsFaults:NO];

回复:故障,这是 Apple 的NSFetchedRequest文档中所说的:

默认值为是。如果结果类型(参见 resultType)是 NSManagedObjectIDResultType,则不使用此设置,因为对象 ID 没有属性值。如果您知道需要从返回的对象访问属性值,则可以将 returnObjectsAsFaults 设置为 NO 以获得性能优势。

核心数据可能非常复杂。如果您只打算使用一次,NSManagedObjectContext请查看 Erica' Sadun 的“核心数据助手”,来自她出色的 iPhone 开发者食谱。如果您要使用 Core Data 做很多事情,我强烈建议您使用它来处理合并嵌套、处理多个线程等MagicalRecord的大量繁重工作: https ://github.com/magicalpanda/MagicalRecordNSManagedObjectContexts

希望这可以帮助!

于 2012-12-01T22:06:01.990 回答