There are several ways to solve synchronization between multiple clients from one data source. Some are simple and inefficient, others efficient but complex. I will cite two approaches.
Periodic check
Marcos' answer uses this technique. In it you create a Timer
or an object that runs at regular intervals, where you then update the values from the server.
This is the simplest and least efficient method. If there is too much data, the system will get blocked frequently and annoy the user. Also, it will transfer a lot of data unnecessarily if they have not changed between one execution and another. This technique is unviable if you need updates in "real time".
One way to improve this would be to run a lighter query to check if there has been any change. For example:
- If only the number of records matters, you can make a
count
table and only update the values if the result is different from the previous one
- Add a field with the creation or modification date of the records and only refer to the records added after the last table update
- Make pagination, just bring the last ones
n
records.
Standard Observer
A more complex scenario would be the implementation of a modification listening mechanism.
For this, no external system should modify the database directly. The Android app and other customers should always update the base via some API, such as a Restful Web Service.
This web service would allow customers to connect to it and receive event updates. So whenever a record was added, all the listeners or observers would be notified.
The desktop application, when started, would remotely connect to the service and keep that connection open, listening for events.
Note that this makes the application much more complex as it needs to deal with several different implementations, a public REST API, and possible communication gaps between client/server.
However, it may be the only way out in a more complex scenario, such as where there is need for instant updates or there are many customers who would overload the database with queries.
Be careful not to break user flow (usability)
Be careful when updating the screen automatically. If the user is looking at the records, or even editing one of them, and everything disappears from the screen, this will generate negative consequences for someone.
If the data is mutable, an idea is to warn the user that there are updates, but only update the table when they click on a button or link.
If the data is immutable, there is not so much problem. Just try to keep the user focus on the same record between updates. For example, if the user selected the record written "banana" and suddenly the focus is on "orange", it will get lost every time the screen changes.
Note on the home screen here from Stack Overflow in English how questions are updated to get an idea about this.
Yes, it is possible. Simply update your jtable’s tablemodel as soon as the insertion is done, this way, every time you delete, edit, or add an item in the table, the tablemodel will update as well. Behold this example, try to edit the names.
– user28595
This when my application sends the value, but when the value is entered into the bank by a third party (not me)... there is some way?
– Leandro Macedo
Stay "listening" to the table and so q is added some value me return that value?
– Leandro Macedo
Third? Desktop application usually has its own basis together with the application, its basis is remote?
– user28595
Yes, the value will be entered by android in the bank and my application should display the value
– Leandro Macedo
It will be like this, the app on android sends an order with products to the database and my desktop application will be the counter, which should display the order as soon as it is registered in the database.
– Leandro Macedo
It’s a desktop app and an android app sharing a remote base?
– user28595
Well, in this case I don’t know any listerner that monitors without having to run manually. Maybe using some routine, but it’s best to wait for someone with more expertise to help you.
– user28595
blz, thanks @Diegofelipe
– Leandro Macedo
If you know some English, see this problem very similar to your Soen
– user28595