简体   繁体   中英

How to handle an arbitrary large amount of data for a table view? (iOS/Objective-C)

I have a situation where I'd like to download more data to display as I scroll down my table view. Currently I figure I could just keep adding the data to an NSMutableArray, and I'm wondering if there's a better way. If I keep scrolling down, my array could potentially get huge and I have a bunch of data sitting there that doesn't even need to be used anymore until I scroll back up.

I've considered the option of releasing and re-downloading parts of the data when appropriate, not sure if this is a common design pattern in dealing with this. The data is very lightweight, just some strings, so the re-downloading option seems viable as the user experience won't get impacted by waiting for the download. More generally, I guess I don't need to worry too much about this specific situation since the data is so lightweight, but it's good to know some general best practices/options anyway.

In this case, I would just keep adding the data to the NSMutableArray. Why?

The amount of memory needed to store UI-related stuff is many times that of simple data classes. That's why UITableView has a reuse mechanism built in from the very first version of iOS. Keep in mind that the user has to make a lot of scroll movements to get to the 1000th row - they probably stop way before that.

If you're really concerned about the memory, you can persist the data with the Core Data framework, and use the NSFetchedResultsController class to do exactly what you describe (load only the visible data objects in memory). Here is a good tutorial on how to do that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM