1

I have a more general question about how to implement lazy loading in my projects for lists, grids, etc.. My question is not about an specific framework or language, because I develop different project types with C#, java, android, winforms, etc.

My question is, how to implement this lazy loading pattern in a RESTful envirorment?

For example, I have a database with about 2 mio datasets. The user select some filters on client side and the server response about 100.000 records. Thats to much to show them all, all over that the time to "load and render" the items in the list control takes several minutes (on bad days). The better way is to show the user the first 200 items, and load the next blocks as needed.

Another example is search for images on a mobile device. If the search result are about 10.000, the mobile traffic for image loading will be exploding. So it's better to show up to 20 entries and if the user scrolls to bottom for example, the next 20 are loaded.

So, how can I archive that on backend / db / client side? Unfortunatly I can't query "select 200 to 400 from table where ...".

On some extern API's I could send a "page token" to get the next block of items. But how do I know which "page" the user ask for?

A bad way I've tried is to load with every request the whole collection (the 100.000 records in first example), and serve only the part which is needed. But this is very ressource wasty when the quantity of clients expands and 10.000 clients do this every minute serveral times.

Marco Rehmer
  • 1,033
  • 2
  • 12
  • 32

1 Answers1

0

You can use pagination for this. For example, the client asks for the first page of x entries, so the server will only fetch that from the DB (see LINQ's skip/take):

DB.Skip(pageNumber * pageSize).Take(pageSize);

The client can alter the filter, pageNumber ("next page") or pageSize without any issues.

Community
  • 1
  • 1
Alexandru Marculescu
  • 5,569
  • 6
  • 34
  • 50