Sqlite离子框架活动会话



我正在尝试打开一个依赖于视图的sqlite数据库。我的代码很好,因为当数据库中存在"Sesion_activa"时,它会显示我想要的视图。但我的问题是,当我打开我的应用程序时,首先显示登录视图,1或2秒后转到相应的视图。

我该如何解决这个问题?当用户登录时,我想把他的html视图默认为

这是我的代码:

App.js

var db = null;//paso 1 BD

角度模块("启动器"、["电子"、"ngCordova"、"启动器控制器"])

.run(function($ionicPlatform, $cordovaSQLite,$state) {//paso 2 agrego cordovaSqlite
  $ionicPlatform.ready(function() {
    if(window.cordova && window.cordova.plugins.Keyboard) {
      // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
      // for form inputs)
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      // Don't remove this line unless you know what you are doing. It stops the viewport
      // from snapping when text inputs are focused. Ionic handles this internally for
      // a much nicer keyboard experience.
      cordova.plugins.Keyboard.disableScroll(true);
      //creo la tabla tutoria
      db = $cordovaSQLite.openDB({ name: 'tuto.db' });//paso 3 creo  la BD
      $cordovaSQLite.execute(db,"CREATE TABLE IF NOT EXISTS tutoria (id integer primary key, estado text, rolUs text, pNombre text, Inicial text, sNombre text, pApellido text, sApellido text, cedula text)");
      //fin creo la tabla tutoria
      //comprobar sesion
      var query = "SELECT * FROM tutoria";
      $cordovaSQLite.execute(db,query).then(function(result) {
        for ( j=0; j < result.rows.length; j++) { 
          if(result.rows.item(j).estado=="Sesion_Activa" && result.rows.item(j).rolUs=="docente"){
            $state.go('tabs.perfilDocente');//HERE GOES TO THE VIEW
          }else{
            if(result.rows.item(j).estado=="Sesion_Activa" && result.rows.item(j).rolUs=="estudiante"){
              $state.go('tabsEst.perfilEstudiante');//HERE GOES TO THE 2cond VIEW
            }
          }               
        }
      });
      //fin comprobar sesion
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
  });
})
//VISTAS
.config(function($stateProvider, $urlRouterProvider) {  
  $stateProvider
    //Login de la APP
    .state('login',{
      cache: false,
      url:'/login',
      templateUrl:'templates/login.html',
      controller: 'LoginCtrl'
    })
    // PADRE DE VISTAS TABS DOCENTE
    .state('tabs',{
      cache: false,
      url:'/Gtuto',
      abstract:true,
      templateUrl:'templates/tabs.html'
    })
    //Hijos de TABS
    .state('tabs.perfilDocente', {
      cache: false,
      url:'/perfil',
      views:{
        'perfil-tab':{
          templateUrl:'templates/perfilDocente.html',
          controller:'SalirCtrl'
        }
      }
    })
    .state('tabs.CompDocente', {
      cache: false,
      url:'/componentes',
      views:{
        'componentes-tab':{
          templateUrl:'templates/CompDocente.html',
          controller:'DocenteCtrl'
        }
      }
    })    
    .state('tabs.ListaTutoDocente', {
      cache: false,
      url:'/componentes/:nom_coe',
      views:{
        'componentes-tab':{
          templateUrl:'templates/ListaTutoDocente.html',
          controller:'DocenteCtrl'
        }
      }
    })
    .state('tabs.CrearTutoria', {
      cache: false,
      url:'/componentes/:nom_coe/:paralelo',
      views:{
        'componentes-tab':{
          templateUrl:'templates/CrearTutoria.html',
          controller:'DocenteCtrl'
        }
      }
    })
    .state('tabs.ContEdicionTuto', {
      cache: false,
      url:'/componentes/:Nom_coe/EdicionTutorias/:id',
      views:{
        'componentes-tab':{
          templateUrl:'templates/ContEdicionTuto.html',
          controller:'DocenteCtrl'
        }
      }
    })
    .state('tabs.comentarios', {
      cache: false,
      url:'/componentes/:componentesId/:c/:f/:d',
      views:{
        'componentes-tab':{
          templateUrl:'templates/comentarios.html',
          controller:'DocenteCtrl'
        }
      }
    })
    .state('tabs.participantes', {
      cache: false,
      url:'/componentes/:componentesId/:c/:f/:d/:e',
      views:{
        'componentes-tab':{
          templateUrl:'templates/participantes.html',
          controller:'DocenteCtrl'
        }
      }
    })
    .state('tabs.notDoc', {
      cache: false,
      url:'/notificaciones',
      views:{
        'notificaciones-tab':{
          templateUrl:'templates/NotDoc.html'
        }
      }
    })
    // PADRE DE VISTAS TABSEST ESTUDIANTE
    .state('tabsEst',{
      cache: false,
      url:'/Gtuto',
      abstract:true,
      templateUrl:'templates/tabsEst.html'
    })
    //Hijos de TABSEST
    .state('tabsEst.perfilEstudiante', {
      cache: false,
      url:'/perfilEst',
      views:{
        'perfil-tabsEst':{
          templateUrl:'templates/perfilEstudiante.html',
          controller:'SalirCtrl'
        }
      }
    })
    .state('tabsEst.CompEstudiante', {
      cache: false,
      url:'/componentesEst',
      views:{
        'componentes-tabsEst':{
          templateUrl:'templates/CompEstudiante.html',
          controller:'AlumnoCtrl'
        }
      }
    })    
    .state('tabsEst.ListaTutoEstudiante', {
      cache: false,
      url:'/componentesEst/:nombre',
      views:{
        'componentes-tabsEst':{
          templateUrl:'templates/ListaTutoEstudiante.html',
          controller:'AlumnoCtrl'
        }
      }
    })
    .state('tabsEst.notificaciones', {
      cache: false,
      url:'/notificacionesEst',
      views:{
        'notificaciones-tabsEst':{
          templateUrl:'templates/NotEst.html'
        }
      }
    })
  $urlRouterProvider.otherwise('/login');
})

angular.run方法是异步的,这导致了所谓的竞争条件。

如果非要我猜测的话,我会说您的$cordova.execute花费的时间太长了,此时您的应用程序已经导航到默认视图,您已将其列为/login。由于angular.run即使在您导航后仍在处理中,因此它将在完成后对您进行导航。这就是为什么你会看到2秒的延迟。

对此的解决方案是不在angular.run方法中运行任何查询或导航逻辑。而是为应用程序初始化创建一个控制器。让这个控制器成为第一个屏幕,并抛出一个加载指示器。初始化逻辑完成后,导航到需要去的地方。

最佳,

最新更新