RAD Studio 10.4.1 is now available! Learn more. Looking for discounts? Visit our Special Offers page!
Delphi

Learn How To Replicate A TFDMemTable Into An IBLite Table With FireDAC In Delphi 10.2 Tokyo

The Field Service Template client app has offline capabilities which allows you to record changes offline without an internet connection. Later updates can be sent back up to the RAD Server once an internet connect is detected. One of the pieces of code in the Field Service Template client app that makes this possible is a function which will take the FireDAC JSON that is received from the RAD Server and store it as a table in IBLite.

As you will see in the source code below the procedure takes a table name, an TFDMemTable, a TFDTable, and a Recreate boolean parameter. In this instance TFDTable is connected to the IBLite database and TFDMemTable contains the FireDAC JSON from the server. The procedure first sets the Table name on the TFDTable. If Recreate is True it will overwrite the existing table if it exists. Next the FieldDefs in the TFDTable get cleared and then copied from the TFDMemTable into the TFDTable. Finally, the data from the TFDMemTable is copied (using CopyDataSet) into the TFDTable.

 

//
//
procedure TMainDM.SaveMemTableToIB(TableName: String; MemTable: TFDMemTable; FDTable: TFDTable; Recreate: Boolean);
begin
     FDTable.Close;
     FDTable.TableName := TableName;
     if Recreate=True then
       begin
         FDTable.FieldDefs.Clear;
         FDTable.FieldDefs.Assign(MemTable.FieldDefs);
         FDTable.CreateTable(True);
         FDTable.CopyDataSet(MemTable, [coStructure, coRestart, coAppend]);
       end
     else
      FDTable.Open;
end;

And that is all there is to it. The data from the server table has now been replicated from the server into the IBLite database on the client. When the server data changes the client data just gets overwritten by the replication. One reason to use IBLite or SQLite this way is that any changes you do make to the data are automatically saved to disk (vs. a TFDMemTable which you would have to manually save). You might also want to use IBLite or SQLite if your data was quite large instead of holding it in a TFDMemTable in memory. Another reason is that if the schema changes on the server it will automatically get updated on the client.

The code to send changes back up to the server is separate. In this instance there is no data syncing going on here because the changes that are made on the client side and sent back to the server are mostly separate from the replicated dataFor syncing data between a client and RAD Server using FireDAC check out this sample.

Check out more information about the Field Service Template in the Deep Dive article about it.


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

Deep Dive Webinar: Boost C++Builder Compile Speed with TwineCompile
About author

FMXExpress.com has over 600 articles with all kinds of tips and tricks for Delphi FireMonkey on Android, IOS, OSX, Windows, and Linux.
Related posts
CodeDelphiRAD Studio

Learn How To Send Arrays To A PostgreSQL Database Server Using FireDAC In Delphi On Windows

C++Delphi

Easily Add Slide Animations To Your Cross Platform Apps With Gesture Support In This FireMonkey Sample

CodeDelphiRAD Studio

TFDBatchMove Is A Powerful Way To Move Data Between Text Files And Tables Using Datasets In Delphi

CodeDelphiRAD Studio

Learn How To Use FireDAC In The Dynamic-Link Libraries With DLL Sharing Sample In Delphi

Leave a Reply

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

IN THE ARTICLES