Quantcast
Channel: Ionic Forum - Latest topics
Viewing all articles
Browse latest Browse all 71531

Many inserts in the same transaction cause crash in app

$
0
0

@fernandoleal wrote:

I have an application that works today 100% online, with data queries directly directed to an API itself.
I'm now implementing the ability to save offline queries in SQLite (with SQLite), so you can access them offline. Everything was being implemented perfectly, even trying to save a query that returns a lot of data, about 20,000 records. This generates in my application 20000 inserts to the database, which ended up causing the crash of my application for "Out of memory", as I could see in the Android console.

I make the 20000 inserts in a single transaction, something like this:

this._http.get(url)
      .map(res => <IndicadoresVendasOffline[]>res.json())
      .catch((err) => {
        console.error(err);
        return Observable.throw(err);
      }).subscribe((result: IndicadoresVendasOffline[]) => {
        console.log(`Result test: ${result.length}`); // print: "Result test: 21657"
        this.sqlDatabase.databaseInstance().then(database => {
          database.transaction((transaction: SQLiteTransaction) => {
            result.forEach(value => {
              transaction.executeSql(`
              INSERT INTO ${IndicadoresVendasOffline.name} (
                  id,
                  emissao,
                  lancamento,
                  previsaoEntrega,
                  cliente,
                  quantidadeTotal,
                  valorLiquido,
                  valorBruto
                ) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
              `,
                this.toDatabase([
                  value.id,
                  value.emissao,
                  value.lancamento,
                  value.previsaoEntrega,
                  value.cliente,
                  value.quantidadeTotal,
                  value.valorLiquido,
                  value.valorBruto
                ])
                , () => {
                  console.log("Ok!");
                 }, this.sqlError
              );
            });
          }).then(() => {
            console.log("Transaction Ok!");
            this.loading = false;
          });
        });
      }, (err) => console.error(err));

How could I have this same behavior without causing "Out of memory" in my app?

Note: I have some limitations, such as: I can not split the request because it must represent the data at the time of the query, and if it splits into 2 or more queries, cross data can occur that does not represent the entire query result in the synchronization.

Posts: 2

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 71531

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>