简体   繁体   English

如何处理表视图的任意大量数据? (式IO /目标C)

[英]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. 目前我想我可以继续将数据添加到NSMutableArray,我想知道是否有更好的方法。 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. 在这种情况下,我会继续将数据添加到NSMutableArray。 Why? 为什么?

The amount of memory needed to store UI-related stuff is many times that of simple data classes. 存储与UI相关的东西所需的内存量是简单数据类的许多倍。 That's why UITableView has a reuse mechanism built in from the very first version of iOS. 这就是为什么UITableView具有从第一版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. 请记住,用户必须进行大量滚动才能进入第1000行 - 他们可能会在此之前停止。

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). 如果您真的关心内存,可以使用Core Data框架保存数据,并使用NSFetchedResultsController类完成您所描述的内容(仅加载内存中的可见数据对象)。 Here is a good tutorial on how to do that. 是一个很好的教程,如何做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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