Using Sqlite on Android with Phonegap

9

Well, I have a little problem ... I'm doing an application and I'm using sqlite on android to save a copy of my database, so far it's fine, it's working, it's copying the data the only problem I found is this.

To make it clear I'm putting the snippet of my code that populates my sqlite database.

var _db;

var DataBase = {

    url : 'http://192.168.1.26:9075/',

    //construtor 
    init: function(){        

        try{
            _db = window.sqlitePlugin.openDatabase(
                {
                    name: "Banco29.db",
                    location: 0 //1
                });
        }catch(e){
            Funcoes.alerta(e,'Erro');
        }

    },

    popularTabela: function(tbl,unidade){
            var nome = tbl.split('.');
            myApp.showPreloader('Atualizando a tabela: '+nome[1]+ ', Isso pode levar alguns minutos, não feche o aplicativo');

            console.log("Populando: "+tbl);
            $.ajax({
                    type: "GET",
                    url: DataBase.url+"procDataBase.php?acao=PopularDados&tabela="+tbl+"&unidade="+unidade,
                    complete:function(){
                     // myApp.hidePreloader();
                    },               
                    success: function (json) {
                        var json = JSON.parse(json);
                        if(json.result == true){
                            var tamanho = json.dados.length;

                           //if(nome[1] === 'ocupacao'){ console.log(JSON.stringify(json)); }

                            for (var i = 0; i < tamanho; i++) {                    
                                if(json.dados[i] !== null && json.dados[i] !== 'null' && typeof(json.dados[i]) !== 'object'){
                                    //console.log(json.dados[i]);
                                    _db.executeSql(json.dados[i], [], function (tx, res){

                                    }, function (t,e){
                                        Funcoes.alerta('Error: '+nome[1]+' '+e.message,'Aviso');
                                    });
                                }
                            }; 
                        }   

                        myApp.hidePreloader();               

                    },error: function(){
                       Funcoes.alerta('Erro para atualizar tabela: '+nome[1]+ ' atualize manualmente a mesma.','Erro');
                       myApp.hidePreloader();  
                    }
            },"json");

        }
}

I make a request $.ajax on my server and it returns me an array of insert, these insert are the data that I am copying from my online database, has table for example that has 5 thousand records, the problem, when I start a request $.ajax I put a "loading" message displaying to the user and within the "success" function of $.ajax I put it to insert the query that my request returned and in the end I put to hide the message myApp.hidePreloader(); , and here is my problem, at the time that the request with the server ends it already hides the message Loading, however the application continues working and inserting the query, logically it should hide the loading message only after you have finished all the processes, right? And my problem is being at this point, because I need the message to only "hide" after finishing, as said has a table that has 5 thousand records and until you finish inserting the application is locked and the person can not move.

Thanks for the help.

    
asked by anonymous 09.10.2015 / 16:06

1 answer

1

In the success callback of _db.executeSql you can check if the last record has been processed and then hide your loading.

// Uso de função imediata (Immediately Invoked Function)
!function db_success(index){
    _db.executeSql(json.dados[index], [], function (tx, res){
        if (index === (tamanho - 1)) {
            Funcoes.alerta('Último registro foi processado');
            myApp.hidePreloader();
        }
    })
}(i), 
function (t,e){
    Funcoes.alerta('Error: '+nome[1]+' '+e.message,'Aviso');
});
    
09.10.2015 / 16:47