Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
News

Collecting Multiple Pages of REST Results in a Single MemTable

the future of starts demands massive productivity

I’ve run into this a few times, where a REST Service returns results in pages, so you have to make multiple passes to get all the data. One thing I love about the RAD REST Client Library is the ability to store all the data in the FDMemTable where you can work on it. Unfortunately when you can only get part of the results that limits the functionality. Here is a simple solution to get all the pages in a single FDMemTable.

What you need is a second FDMemTable. My first one is called MembersTempTable, and it is attached to the Response Adapter. The second one is called MembersMemTable and it will hold the complete set of data.

/ Drop any existing data
MembersMemTable.Close; 
 
// Get the first page 
MembersRequest.Params.ParameterByName('limit').Value := '50';
MembersRequest.Params.ParameterByName('offset').Value := '0';
MembersRequest.Execute;
 
// This clones the FDMemTable
MembersMemTable.CopyDataSet(MembersTempTable, 
  [coStructure, coRestart, coAppend]);
 
// Now we add the other tables
MembersMemTable.BeginBatch();
try
  // Repeat until we don't get a full page
  while MembersTempTable.RecordCount = 50 do
  begin
    // Start with the next page
    MembersRequest.Params.ParameterByName('offset').Value := 
      (MembersMemTable.RecordCount - 1).ToString;
    MembersRequest.Execute;
    // Append those records into our FDMemTable
    MembersMemTable.CopyDataSet(MembersTempTable, [coAppend]);
  end;
  MembersMemTable.IndexFieldNames := 'id';
finally
  MembersMemTable.EndBatch;
end;

 

See What's New in 12.2 Athens See What's New in 12.2 Athens Dev Days of Summer 2-24

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

About author

Director of Delphi Consulting for GDK Software USA. Many software related patents, including swipe and pattern unlock and search engines. First Silver and Gold Delphi badges on Stack Overflow Former Developer Advocate for Embarcadero Technologies. Long time fan of programming, especially with Delphi. Author, Podcaster/YouTuber, Improvisor, Public Speaker, Father, and Friend.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES