I am about to finish the creation of an APP, which consumes data from a REST API. This application, works with CLIENTS, ORDERS and PRODUCTS, brings them from the REST API and inserts them into the local database (SQLite).
I would need to know what efficient way there is to store the data that comes in an array? That is to say, the first time, it brings me 48,000 clients, within an array of arrays. The problem is that when you insert ITEM by ITEM, the application is labeled, or takes a long time to do so.
I would need to know what is the most efficient way to do this.
I speak of "only the first time", because based on the highest ranks, the next synchronization, I do it from then on (and it only brings the updated ones or the new ones).
The query is:
'INSERT OR REPLACE INTO orders (id, C_IDPEDIDO, N_NROPEDIDO, C_CLIENTE, N_NROAUTORIZACION, FH_AUTORIZACION, FH_BAJA, C_IDFACTNOTA, N_ESTADO, X_OBSERVACION, FH_INCORPORACION, ORA_ROWSCN, state, error) VALUES ((SELECT id FROM orders WHERE C_IDPEDIDO=?), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
But, as I mentioned, when inserting one by one, or it is marked or takes a lot.
When returning "promises", those queries I wait for them (to end) with an "Observable.ForkJoin" and when I finish all, I give the synchronization finished
Here I go through the clients that came from the webservice in "clients: []"
The code is:
let promises = [];
for(let key = 0; key < clients.length; key++) {
// Insertamos el cliente.
promises.push(this.SQLite_query('INSERT OR REPLACE INTO clients (id, C_CLIENTE, X_APELLIDOCLI, X_NOMBRES, X_DOMICCLI, X_LOCALIDAD, X_PROVINCIA, X_NROCUIT, X_CONDICIONIMP, ORA_ROWSCN) VALUES ((SELECT id FROM clients WHERE C_CLIENTE=?), ?, ?, ?, ?, ?, ?, ?, ?, ?)', [clients[key]['C_CLIENTE'], clients[key]['C_CLIENTE'], clients[key]['X_APELLIDOCLI'], clients[key]['X_NOMBRES'], clients[key]['X_DOMICCLI'], clients[key]['X_LOCALIDAD'], clients[key]['X_PROVINCIA'], clients[key]['X_NROCUIT'], clients[key]['X_CONDICIONIMP'], clients[key]['ORA_ROWSCN']]).then((data) => {
// "{"rows":{"length":0},"rowsAffected":1,"insertId":1}
if(data['rowsAffected']) {
// Si se insertó.
ret['inserted'].push(clients[key]['C_CLIENTE']);
} else {
// Si no se insertó, lo agregamos a los existentes.
ret['existents'].push(clients[key]['C_CLIENTE']);
}
}, (error) => {
// Si hubo un error, lo agregamos a errores.
ret['error'].push(clients[key]['C_CLIENTE']);
}));
}
Observable.forkJoin(promises).subscribe((data) => {
// When all the promises are over, we return the data.
resolve(ret)
});
Please, I would need to be able to solve that since I eat my time.
I am stuck in that part that is the only one that I would lack to be able to complete it.
Of course, thank you very much to everyone!