Keep Equal Data

3

I'm doing an Chat on Android.

I'm using Servlet (with JSON) + Oracle + Android.

When I add a contact, for example, I save that contact in the Oracle database, via Servlet.

But at the time of viewing the contacts in a ListView , if I fetch this data via JSON from the server, it might be that the user does not have an internet and thus he would not be able to see his contacts. p>

What would be the best solution for this?

Create a local bank and create a service that makes the two banks the same?

I accept suggestions.

    
asked by anonymous 21.10.2016 / 02:13

2 answers

8

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.

        
    23.10.2016 / 20:46
    2

    Give priority to saving data locally rather than saving it to an external server.

    The reasons are in the question itself. If there is no internet connection, you will not even be able to add new contacts or simple actions like editing an existing contact, deleting, etc.

    I suggest you save locally and create options to "export" to an external medium, which can be a txt (csv) file, bluetooth send, database, etc.

    That's even what all apps do.

    I believe there is no need to show codes because the doubt seems to be only conceptual.

        
    27.10.2016 / 07:29