Update Changes in a local dataset when another client app changes data

Having a local dataset open in app 1 want to raise an event on the server when App 2 updates this data, and allow me to Gte only the changes and incorporate to App1 local dataset. Reopening dataset is not a desirable option because have many records.

What is the best way to do?


Obviously the approach would be

  1. Gather update information
  2. Send this information back to the caller

Assuming you are using Delphi you’d need to:

  1. Gather information about Deltas to be processed using the OnBeforeProcessDeltas event.
  2. In the OnAfterProcessDeltas event (wait for this event to be sure that no exceptions were raised during the update processing) pack the Deltas into a Binary stream (there is a method named PackDeltas in the TDataAbstractService class, you’ll need to create a similar one as this one is protected)
  3. Send out an event using the IDataChangeNotification.OnDataTableChanged event notification (be sure to exclude the current user from the recipients list as it already has these changes)
  4. Client-side when such event is received unpack deltas from the Binary stream (again there is a similar method in the TDataAbstractService class) and apply these deltas to your local dataset. Check type for each delta change and either remove the data row from the dataset or update its field values based on the DeltaChange’s values.

If the table is rarely changed then you could sent back from the server only the primary key values of the rows being changed and then query server for them. This approach requires way less code and is much simpler, but it will result in increased workload on the server (as every client will ask the server for values of these changed rows once a change occurs)

Hope that helps

I assume that must be on a second dataset. How to merge both datasets then? Manually ? Or theres a better method?

Bets regards.

You’ll need to do this manually. Also please remember that changing rows will mark them as changes, so they would be added to the Delta next time you’ll apply changes to the server. You’ll need to pause change tracking before merging rows and resume it once merging ends (via the https://docs.dataabstract.com/API/Delphi/Classes/TDADataTable/#LogChanges property).

1 Like

you can use these methods of table:

2nd one can use DynamicWhere expression where you can specify primary key(s)