How to load a huge amount of data from a web service in a UITableView?

I am currently creating an application that will have a UITableView that will receive data from a web service. The returned data may contain a large amount of records, for example 50,000 records. I know that all records cannot be immediately loaded into the array, this will cause the application to not respond due to lack of memory. The web service I call gets the parameters for page size and page number, so it only returns the data that I want to show to the user. But I did not find any method that tells me that the user has scrolled the entire displayed record and should get new data from this service. The screen also has a search bar and a search button, which, when clicked, calls another service that returns a search result. This will have the same problem as before, as the result can bring many records.

What would be the best way to do this?

+4
source share
2 answers

Well, there are several approaches to this problem. One of the most popular is the implementation of the three20 framework (table view controllers supporting Internet access)

As for your second question, the tableview delegate has a convenient method:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 

UPDATE:

Exactly one year after the publication of this answer, I recommend that you use RestKit . Many people think that it has a steep learning curve, but it’s really a great base for processing residual resources and includes cache management, integration / mapping of master data and, among other things, a page (paginator) that pretty much covers all your requirements.

+7
source

We are currently doing something similar in our application. Since UITableView is inherited from UIScrollView, you probably want to use some of the properties and methods of UIScrollView. You will almost certainly want to implement a class that matches the UIScrollView delegate. Then, when the user scrolls to the bottom of the currently loaded entries in the UITableView, you can request a web service for the next data page. The documentation for UIScrollView is here and the documentation for UIScrollViewDelegate is here .

Here is an example implementation of the scrollViewDidScroll: method scrollViewDidScroll: UIScrollViewDelegate, which should get the behavior of loading records when the user reaches the bottom of the scroll.

 - (void) scrollViewDidScroll:(UIScrollView *) scrollView { // Calculate whether the user has reached the bottom of the scroll view. CGPoint offset = scrollView.contentOffset; CGRect bounds = scrollView.bounds; CGSize size = scrollView.contentSize; UIEdgeInsets inset = scrollView.contentInset; float y = offset.y + bounds.size.height - inset.bottom; float h = size.height; float reload_distance = 0; if (y >= h + reload_distance) { // Request next page of data here. } } 

The beauty of UITableViewDataSource methods is that they do not require your data to be stored in memory. You can provide the necessary data for each cell requested on demand when tableView:cellForRowAtIndexPath: called. Similarly, you can provide a value to return from tableView:numberOfRowsInSection: without actually having all the data for the rows in memory. However, you will most likely want to save cell data around the memory cells you are viewing in memory to ensure reasonable performance. Unfortunately, all this can be quite complicated, but it should be possible. You can find more documentation on the UITableDataSource here .

+3
source

Source: https://habr.com/ru/post/1401616/


All Articles