jeudi 28 mai 2015

Function executing all together - jQuery Mobile and Cordova

I apologize in advance if my doubt not be clear enough ... Before I say my doubts explain what I'm doing.

I'm developing an application that performs a connection cord in a webservice and creates a database offline with sqlite. The application is working the problem is being in the execution of my duties I created, they are running all together and ends up crashing the application. Example have a table in my database which has 1000 records, to load all the records in SQLite database takes a while and this time my application is locked.

I'm using anonymous functions in javascript. its is my file js for connection database with webservice:

var _db;

var DataBase = {

    url : 'http://ift.tt/1FGxJTE',
    tabelaExiste: false,
    tabelaName : '',
    controleA: 0,
    controleB: 0,

    //construtor 
    init: function(){
        try{
            _db = window.sqlitePlugin.openDatabase(
                {
                    name: "DATABASE.db",
                    location: 1 //1
                });
        }catch(e){
            Funcoes.alerta(e,'Erro');
        }
        //
        DataBase.criarBanco();
    },

    //cria o banco
    criarBanco: function(){
    
       var arr = ['user','states','city','unit','test'];

        arr.forEach(function(valor,chave){
            //alert(valor);
            DataBase.criarTabela(valor);
        });        
    },

    criarTabela: function(tbl){    
        nome = tbl.split('.');

        Funcoes.showLoad('Aguarde um momento, estamos atualizando o aplicativo'); 
        alert("Criando a tabela: "+nome[1]);
        $.ajax({
                type: "GET",
                url: DataBase.url+"Dropbox/procDataBase.php?acao=CriarTabelas&tabela="+tbl,
                async: false,                
                dataType: "json", 
                success: function (json) {
                    
                     _db.transaction(function (tx) {
                        tx.executeSql('DROP TABLE IF EXISTS '+nome[1]);
                        tx.executeSql(json.query, [], function (tx, res){
                           DataBase.popularTabela(tbl);
                        }, function (t,e){
                            Funcoes.alerta('Error: '+e.message,'Aviso');
                        });
                    }); //fim transaction create table

                    Funcoes.hideLoad();
                },error: function(){
                   Funcoes.alerta('Erro para iniciar o banco de dados','Erro');
                   Funcoes.hideLoad();
                }

        });

    },

    popularTabela: function(tbl){
        nome = tbl.split('.');
        Funcoes.showLoad();  
        alert("Inserindo dados: "+nome[1]);     
        
         //alert("Populando a tabela: "+tbl);
        $.ajax({
                type: "GET",
                url: DataBase.url+"Dropbox/procDataBase.php?acao=PopularDados&tabela="+tbl,
                async: false,                
                dataType: "json", 
                success: function (json) {
                    
                    if(json.result == true){
                        tamanho = json.dados.length;

                        for (var i = 0; i < tamanho; i++) {
                            
                           //_db.transaction(function (tx) {
                                _db.executeSql(json.dados[i], [], function (tx, res){

                                }, function (t,e){
                                    Funcoes.alerta('Error: '+e.message,'Aviso');
                                });
                            //}); //fim transaction create table
                        };  
                    }                  

                   Funcoes.hideLoad();
                },error: function(){
                   Funcoes.alerta('Erro para iniciar o banco de dados','Erro');
                   Funcoes.hideLoad();
                }

        });

    }

};

Another detail I noticed was the following when I call a function, my role I created "showLoad" it should show a message wait, does not appear, it seems she runs and has desaperece, that way you do not know you need to wait for the end of execution of my seat and then move the application ....

My function showLoad:

var Funcoes = {

    //mostrar Loading
    showLoad: function(mensagem){         
        msg = mensagem == '' || typeof(mensagem == 'undefined') ? 'Aguarde um momento' : mensagem;
        $('#block-ui').show();        
        $.mobile.loading('show', {theme:"e", text:msg, textonly:false, textVisible: true});
    }

};

In summary I would like to know: - Because the functions create table and PopularDados are running at the same time? correct would create the table and then call the function PopularDados, and so one by one, but not doing so, first create all the tables (ignores the function of popularDados and then populates all the tables at the same time ...) - Because the function showLoad is not displayed, it is ignored, it does not show the message wait ...

Thanks very much!

Aucun commentaire:

Enregistrer un commentaire