Dailycode.info

Short solution for short problems

iOS: UITable view (Server Side) Lazy Loading

I'm creating an app that will be used in factories. At the test factories, the application runs fast and smooth. But when starting to deploy to other factories where the workflow is different, I had some grids with loading times of 5 to 7 seconds. They didn't liked it and so did I. So then I had to build in paging in the grids. Since I'm working with a .Net backend server, I will post this answer using LINQ on the server and iOS on the app.

The solution is to show the first 50 elements in the grid, when the 50st element is shown, he has to get the next 50 element and so on... The implementation is actually very simple. I pass the itemCount and the pageSize to the server. So you will need to add these arguments to the getter of your data. On server side I just add a simple Skip and Take using link. Skip the first ... items and Take the (pageCount) number of items. Thats it. So it took me 30 minutes to implement it for the first grid, the next will be 5 minutes of work. 

First on the app side, add this variable

int iPageSize=0;

Then in the CellForRowAtIndexPath method you''l need to check the number of items and load the following items before you return the cell. The store.PageItemCount is a global variable that defines the max items per page, I made it global so it can be used on all grids and variable so we can change the item size on the device.

int indexRow = indexPath.row+1;

int iPageSizeSum = iPageSize+store.PageItemCount;

if (indexRow >= iPageSizeSum)

{

    iPageSize = iPageSize + store.PageItemCount;

    [self configureView];

}

 

in the configureView I add the items to a mutable array and reload the table:

    

[self.ordersOnJig addObjectsFromArray:(NSMutableArray *) value];

[self.locationsTable reloadData];

[self.locationsTable reloadInputViews];

Now the server side integration is even more simple:

returnList = returnList.Skip(skip).Take(pageSize).ToList();

return returnList;