Well, as it is a question based on opinions, I will expose mine.
First of all I would do a local bank (SQLite).
This bank would be a mirror of my remote bank.
To ensure greater data security and synchronization, I would create a background service every time the application is open.
What would my service do?
Get a list of local contacts (SQLite).
Send this list to a webservice.
Upon receiving the list, have an algorithm that scans all received contacts by comparing them with contacts already saved.
If there is a difference in contacts, do whatever is necessary to update both sides.
In this case, I can have two situations: The local bank is larger than the remote (which forces me to do an update of the remote). Or the remote bank is larger than the location. (which forces me to do an upgrade of the site).
In any of these cases you will do the same thing: Synchronize the banks.
As you mentioned that you are working with servlets, you can use removeAll
to compare two lists (the local and remote database list).
Collection listaRemota = new ArrayList() {{
add("maca");
add("laranja");
}};
Collection listaLocal = new ArrayList() {{
add("maca");
add("laranja");
add("coco");
add("morango");
}};
// Remover os elementos
listaLocal.removeAll(listaRemota);
// Exibir resultado
System.out.println("Resultado: " + listaLocal);
Resultado: ["coco", "morango", "laranja"]
Remembering that as your case is a comparison (complex or otherwise) of objects. You will need to define a comparison rule (overloading the equals()
method) to use such a mechanism.
Another example implementation
You can also stack all actions on top of any contact. This way you will always have the largest data mass in your local database (SQLite) and guarantee synchronization every time the mobile device has connectivity to the internet.
You can create a table of type:
CREATE TABLE sincronizacao (id INTEGER PRIMARY KEY, ENTIDADE varchar(20), ACAO varchar(1), VALOR varchar(500), SINCRONIZADO INTEGER, DATA_SINCRONIZAR INTEGER);
You can do a generic method for each action taken on top of a contact ( INSERT
, UPDATE
, DELETE
) that will save the contact's "state" - or value -:
insert into sincronizacao (entidade, acao, valor, sincronizado, data_sincronizar) values ('CONTATO', 'I', '{ "telefones": [ "(21) 91111-2222", "(21) 92477-1515" ], "nome": "Stack Overflow PT", "null": null, "endereco": "Brasil", "object": { "a": "b", "c": "d", "e": "f" }, "outra_propriedade": "Olá, mundo!"}', 0, CURRENT_TIMESTAMP)
The above value is a JSON, that is, you can do serialization and deserialization on any of the "sides":
{
"telefones": [
"(21) 91111-2222",
"(21) 92477-1515"
],
"nome": "Stack Overflow PT",
"null": null,
"endereco": "Brasil",
"object": {
"a": "b",
"c": "d",
"e": "f"
},
"outra_propriedade": "Olá, mundo!"
}
Then I make a change in some property of that same contact and make another record in my table:
insert into sincronizacao (entidade, acao, valor, sincronizado, data_sincronizar) values ('CONTATO', 'U', '{ "telefones": [ "(21) 91111-2222", "(21) 92477-1515" ], "nome": "Stack Overflow PT", "null": null, "endereco": "Brasil", "object": { "a": "b", "c": "d", "e": "f" }, "outra_propriedade": "Olá, mundo! Essa propriedade foi alterada."}', 0, CURRENT_TIMESTAMP)
VALOR
in the "family" view:
{
"telefones": [
"(21) 91111-2222",
"(21) 92477-1515"
],
"nome": "Stack Overflow PT",
"null": null,
"endereco": "Brasil",
"object": {
"a": "b",
"c": "d",
"e": "f"
},
"outra_propriedade": "Olá, mundo! Essa propriedade foi alterada."
}
You can also create a trigger that does this.
Anyway, in the end you will do the same thing:
Get a list of unrealized synchronizations:
select * from sincronizacao where sincronizado = 0;
Send a list of objects (after deserializing them) to a webservice. In webservice, it follows the same treatment flow in the increasing order of data_sincronizar
.
In the case above, notice that I first did an "I" action ( insert
) and then did another "U" action ( update
). In the list handling on the webservice side, I will execute the actions according to the order they were "created". Then on my remote server, I would first make a insert
of the deserialized object. And then it would make a update
.
Returning an "OK" from the webservice, you "scan" the same sent list and make an update of your control column sincronizado
:
update sincronizacao set sincronizado = 1 where id in (string_delimitada_por_virgula_com_cada_id_atualizado_no_ws);
And that's it!
This way you also always guarantee the local update (which is more important in your case) and also ensures the synchronization and security of this data by storing the information on a remote server, since, for chat use, it is necessary to connect to the internet.